如何使用 Pandas DataFrame plot 函数为每个子图绘制 ylabel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31760509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:42:57  来源:igfitidea点击:

How to plot a ylabel per subplot using pandas DataFrame plot function

pythonpandasmatplotlibplotsubplot

提问by Jacques MALAPRADE

By default pandas.DataFrame.plot() using the subplots option doesn't seem to make it easy to plot a ylabel per subplot. I am trying to plot a pandas dataframe having a subplot per column in the dataframe. Code so far that doesn't work:

默认情况下,使用 subplots 选项的 pandas.DataFrame.plot() 似乎并不能轻松地为每个子图绘制 ylabel。我正在尝试绘制一个 Pandas 数据框,数据框中的每列都有一个子图。到目前为止不起作用的代码:

fig = plt.figure(figsize=(10,10))
ax = plt.gca()
df.plot(y=vars, ax=ax, subplots=True, layout=(3,1), sharex=True, legend=False,)
ax.set_ylabel = ['y','x', 'z']

But this doesn't plot any labels at all.

但这根本没有绘制任何标签。

回答by Jianxun Li

You can set y label on each ax separately.

您可以分别在每个轴上设置 y 标签。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# data
df = pd.DataFrame(np.random.randn(100,3), columns=list('ABC'))

# plot
axes = df.plot(figsize=(10, 10), subplots=True, sharex=True, legend=False)
axes[0].set_ylabel('yA')
axes[1].set_ylabel('yB')
axes[2].set_ylabel('yC')

enter image description here

在此处输入图片说明