gpt4 book ai didi

python - 如何定义 Pandas 中绘制图形的范围?

转载 作者:行者123 更新时间:2023-11-28 17:38:47 25 4
gpt4 key购买 nike

我现在正在尝试通过 Pandas 绘制数据框。一切都很好,但我不知道如何定义 y 轴和 x 轴刻度。例如,在下面,我想显示 y 轴刻度从 1.0 到 0.0 而不是 0.0 到 0.7 的图表。

enter image description here

这是上图的代码。

In [90]: df
Out[90]:
history lit science social accuracy
2014-11-18 0.680851 0.634146 0.452381 0.595745 0.01
2014-12-10 0.680851 0.634146 0.452381 0.595745 0.01

In [91]: df.plot()
Out[91]: <matplotlib.axes._subplots.AxesSubplot at 0x7f9f3e7c9410>

此外,我想为每个点显示标记“x”。例如,DataFrame df 有两行,因此我想为图表上的每个点标记“x”或“o”。

更新:

在应用了Ffisegydd的大解之后,得到了我原本想要的下图。

In [6]: df.plot(ylim=(0,1), marker='x')

enter image description here

最佳答案

pandas.DataFrame.plot()将返回一个 matplotlib 轴对象。这可用于使用 ax.set_ylim() 修改诸如 y 限制之类的内容。

或者,当您调用 df.plot() 时,您可以为样式传入参数,这些参数之一可以是 ylim=(minimum_value, maximum_value),这意味着您不必在绘图后手动使用 ax.set_ylim()

您还可以传递传递给 matplotlib 绘图例程的额外关键字参数,您可以使用它来将标记设置为 xmarker='x' .

下面给出了一个玩具示例,在 df.plot( ) 调用。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(data={'x':[0,1,2,3,4], 'y':[0,0.5,1,1.5,2]})

ax = df.plot(ylim=(0,5), marker='x')

plt.show()

Example plot

关于python - 如何定义 Pandas 中绘制图形的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com