当使用`secondary_y'时,如何更改pd.dataframe.plot()的传奇字体大小?
问题
- 我在pd.dataframe.plot()中使用了
Secondary_y
参数。 - 在尝试通过
.legend(fontsize = 20)
更改传奇的字体时,当我实际上在传说上打印2列时,我最终在图例中只有1列名称。 - 当我不使用
Secondary_y
参数时,此问题(传说中只有1列名称)不会发生。 - 我希望我的数据框中的所有列名在图例中打印,即使在绘制dataframe时使用
secondary_y
时,也会更改传说的字体大小。
示例
- 以下示例使用
secondary_y
仅显示1列名称a
,当我实际上有2列时,a
和b
。 - 传说的字体大小被更改,但仅用于1列名称。
import pandas as pd
import numpy as np
np.random.seed(42)
df = pd.DataFrame(np.random.randn(24*3, 2),
index=pd.date_range('1/1/2019', periods=24*3, freq='h'))
df.columns = ['A', 'B']
df.plot(secondary_y = ["B"], figsize=(12,5)).legend(fontsize=20, loc="upper right")
- 当我不使用
Secondary_y
时,Legend显示了两个列a
和b
。
import pandas as pd
import numpy as np
np.random.seed(42)
df = pd.DataFrame(np.random.randn(24*3, 2),
index=pd.date_range('1/1/2019', periods=24*3, freq='h'))
df.columns = ['A', 'B']
df.plot(figsize=(12,5)).legend(fontsize=20, loc="upper right")
Question
- I have used the
secondary_y
argument in pd.DataFrame.plot(). - While trying to change the fontsize of legends by
.legend(fontsize=20)
, I ended up having only 1 column name in the legend when I actually have 2 columns to be printed on the legend. - This problem (having only 1 column name in the legend) does not take place when I did not use
secondary_y
argument. - I want all the column names in my dataframe to be printed in the legend, and change the fontsize of the legend even when I use
secondary_y
while plotting dataframe.
Example
- The following example with
secondary_y
shows only 1 column nameA
, when I have actually 2 columns, which areA
andB
. - The fontsize of the legend is changed, but only for 1 column name.
import pandas as pd
import numpy as np
np.random.seed(42)
df = pd.DataFrame(np.random.randn(24*3, 2),
index=pd.date_range('1/1/2019', periods=24*3, freq='h'))
df.columns = ['A', 'B']
df.plot(secondary_y = ["B"], figsize=(12,5)).legend(fontsize=20, loc="upper right")
- When I do not use
secondary_y
, then legend shows both of the 2 columnsA
andB
.
import pandas as pd
import numpy as np
np.random.seed(42)
df = pd.DataFrame(np.random.randn(24*3, 2),
index=pd.date_range('1/1/2019', periods=24*3, freq='h'))
df.columns = ['A', 'B']
df.plot(figsize=(12,5)).legend(fontsize=20, loc="upper right")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个较晚的响应,但是对我有用的东西只是设置
plt.legend(fontsize = want_fontsize)
之后。this is a somewhat late response, but something that worked for me was simply setting
plt.legend(fontsize = wanted_fontsize)
after the plot function.要设法自定义它,您必须使用matplotlib的子图函数创建图形:
结果:

To manage to customize it you have to create your graph with subplots function of Matplotlib:
result:

我设法解决了这个问题,但我的传说分为两个部分:
I managed to kind of solve the problem, but my legend is divided into two parts :