当使用`secondary_y'时,如何更改pd.dataframe.plot()的传奇字体大小?

发布于 2025-02-10 07:29:31 字数 1547 浏览 0 评论 0原文

问题

  • 我在pd.dataframe.plot()中使用了Secondary_y参数。
  • 在尝试通过.legend(fontsize = 20)更改传奇的字体时,当我实际上在传说上打印2列时,我最终在图例中只有1列名称。
  • 当我不使用Secondary_y参数时,此问题(传说中只有1列名称)不会发生。
  • 我希望我的数据框中的所有列名在图例中打印,即使在绘制dataframe时使用secondary_y时,也会更改传说的字体大小。

示例

  • 以下示例使用secondary_y仅显示1列名称a,当我实际上有2列时,ab
  • 传说的字体大小被更改,但仅用于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显示了两个列ab
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 name A, when I have actually 2 columns, which are A and B.
  • 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")

enter image description here

  • When I do not use secondary_y, then legend shows both of the 2 columns A and 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")

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

农村范ル 2025-02-17 07:29:31

这是一个较晚的响应,但是对我有用的东西只是设置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.

ぃ双果 2025-02-17 07:29:31

要设法自定义它,您必须使用matplotlib的子图函数创建图形:

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

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']

#define colors to use
col1 = 'steelblue'
col2 = 'red'

#define subplots
fig,ax = plt.subplots()

#add first line to plot
lns1=ax.plot(df.index,df['A'],  color=col1)

#add x-axis label
ax.set_xlabel('dates', fontsize=14)

#add y-axis label
ax.set_ylabel('A', color=col1, fontsize=16)

#define second y-axis that shares x-axis with current plot
ax2 = ax.twinx()

#add second line to plot
lns2=ax2.plot(df.index,df['B'], color=col2)

#add second y-axis label
ax2.set_ylabel('B', color=col2, fontsize=16)

#legend
ax.legend(lns1+lns2,['A','B'],loc="upper right",fontsize=20)

#another solution is to create legend for fig,:
#fig.legend(['A','B'],loc="upper right")

plt.show()

结果:

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

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

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']

#define colors to use
col1 = 'steelblue'
col2 = 'red'

#define subplots
fig,ax = plt.subplots()

#add first line to plot
lns1=ax.plot(df.index,df['A'],  color=col1)

#add x-axis label
ax.set_xlabel('dates', fontsize=14)

#add y-axis label
ax.set_ylabel('A', color=col1, fontsize=16)

#define second y-axis that shares x-axis with current plot
ax2 = ax.twinx()

#add second line to plot
lns2=ax2.plot(df.index,df['B'], color=col2)

#add second y-axis label
ax2.set_ylabel('B', color=col2, fontsize=16)

#legend
ax.legend(lns1+lns2,['A','B'],loc="upper right",fontsize=20)

#another solution is to create legend for fig,:
#fig.legend(['A','B'],loc="upper right")

plt.show()

result:
enter image description here

倾城花音 2025-02-17 07:29:31

我设法解决了这个问题,但我的传说分为两个部分:

fig, ax = plt.subplots()

# your code with your plots

ax.legend(['A'], fontsize=15)
ax.right_ax.legend(['B'], fontsize=15)

I managed to kind of solve the problem, but my legend is divided into two parts :

fig, ax = plt.subplots()

# your code with your plots

ax.legend(['A'], fontsize=15)
ax.right_ax.legend(['B'], fontsize=15)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文