我正在用 pandas 打印各种东西,使用内置的 plot
命令,例如 my_dataframe.plot()
后跟 plt.show()
在 ipython 中。
现在这是一种非常简单方便的可视化方式,并且考虑到我无论如何都对 SVG 文件进行后处理,所以我不太关心绘图的细节。
但是,我需要标题、图例和绘图上 x 轴和 y 轴的标签,既可以提醒自己什么是什么,也可以快速将一些东西发送给其他人,而无需添加“哦,顺便说一句,这次 x 轴是小时,y 轴一如既往地是米,但蓝色现在是样本 B……”电子邮件行。
我想出了如何以一种简单的方式做到这一点(见下文),我也知道我可以用 ax
做的各种强大的事情,但我花了一段时间才明白到我的“简单”解决方案,我远离 ax
因为有太多我既不需要也不理解的东西。
我确实理解为什么人们会想要 ax
的所有强大选项,但我不明白为什么 pandas 绘图功能中没有包含这样一个简单的选项。而且好像我不是唯一一个。例如,用户 Chrispy 发布了这条评价很高的评论:
is there a particular reason why x and y labels can't be added as arguments to
pd.plot()
? Given the additional concision ofpd.plot()
overplt.plot()
it seems it would make sense to make it even more succinct instead of having to callax.set_ylabel()
关于 this question 的答案,但没有得到进一步的评论。因此,我公然窃取了这个问题。
为什么 plt.plot()
默认包含图例并且还可以让我轻松添加标题 (my_df.plot(title = 'check out my cool plot')
),但合乎逻辑的下一步 (my_df.plot(ylabel = 'size in meters')
) 导致 TypeError: There is no Line2D property "ylabel"
?
我是否遗漏了什么或是否有这种疏忽的原因?
示例代码:
当我在我的真实文件中实现它并在 ipython 中使用 run workflow.py
运行它时,这是有效的,但是当复制粘贴代码时我无法重现它。要么我的标签被忽略,要么彻底失败:
编辑:
最初我在此处的示例中有 plt.xlabel = 'time in seconds'
,但它不起作用,但我有正确的 plt.xlabel('time in seconds')
在我的真实代码中,这当然有效。
times = np.arange(0,43200,60)
my_df = pd.DataFrame(np.random.randn(len(times)), index = times)
my_df.plot(title = 'just some random data') #this works
#my_df.plot(title = 'just some random data', ylabel = 'size in meters', xlabel = 'time in seconds') #this seems like the logical next step, but it errors
plt.ylabel('size in meters')
plt.xlabel('time in seconds')
这似乎是使用@Johannes 解决方案的axes
最简单/最小的解决方案,但我认为这(请参阅对答案的评论)也很好地说明了为什么我不想用 axes
打扰自己:
axes = my_df.plot(title = 'just some random data')
axes.set_ylabel('size in meters')
axes.set_xlabel('time in seconds')
此外,我可以用另一种方式设置标题,但标签只有一个选项,这让我感到困惑:
axes = my_df.plot()
axes.set_title('just some random data')
axes.set_ylabel('size in meters')
axes.set_xlabel('time in seconds')
最佳答案
首先,pandas 绘图命令没有特别的理由不包括标签的关键字参数,就像标题一样。
此选项可以很好地实现,但事实并非如此。推测原因不会导致任何问题,但在 pandas issue tracker 上有一个问题。 .
关于实际问题,有几种方法可以为坐标轴设置标签。下面列出了三种可能的方法。请注意,在问题和其他答案中出现了一些无效的方法。
特别是 ax.xlabel()
不存在。 plt.ylabel = 'size in meters'
也没有任何意义,因为它覆盖 ylabel
方法而不是使用 它。
工作选项:
ax.set_xlabel()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
times = np.arange(0,43200,60)
my_df = pd.DataFrame(np.random.randn(len(times)), index = times)
ax = my_df.plot(title = 'just some random data')
ax.set_ylabel('size in meters')
ax.set_xlabel('time in seconds')
plt.show()
ax.set()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
times = np.arange(0,43200,60)
my_df = pd.DataFrame(np.random.randn(len(times)), index = times)
ax = my_df.plot(title = 'just some random data')
ax.set(xlabel='time in seconds', ylabel='size in meters')
plt.show()
plt.xlabel()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
times = np.arange(0,43200,60)
my_df = pd.DataFrame(np.random.randn(len(times)), index = times)
my_df.plot(title = 'just some random data')
plt.ylabel('size in meters')
plt.xlabel('time in seconds')
plt.show()
plt.setp()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
times = np.arange(0,43200,60)
my_df = pd.DataFrame(np.random.randn(len(times)), index = times)
ax = my_df.plot(title = 'just some random data')
plt.setp(ax,xlabel='time in seconds', ylabel='size in meters')
plt.show()
关于python - 为什么我不能将 x 和 y 标签设置为 pd.plot() 的参数,而我可以轻松设置类似的东西,例如标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44922413/