x 轴被 pandas.plot(...) 意外反转 [英] x-axis inverted unexpectedly by pandas.plot(...)
问题描述
在使用 Pandas 绘制一系列数据与另一系列数据时,X 轴自动且意外地反转.请在下面查看我的代码.如何确保 x 轴始终指向右侧?x 轴的这种自动反转是熊猫的预期行为吗?可以禁用吗?
X axis was inverted automatically and unexpectedly when plotting a series of data against another series of data using pandas. Please look at my code below. How can I ensure x axis is always pointing to the right? Is this automatic inversion of x axis an intended behavior of pandas? Can it be disabled?
让我在下面解释我的示例.创建了三个图.我希望每一个都显示出向右上升的近 45 度线.然而,其中一些有 45 度线向右下降,因为它的 x 轴自动反转.看来x轴是否反转取决于要绘制的值.
Let me explain my example below. Three plots are created. I expect each one shows nearly 45 degree line rising to the right. However, some of them have 45 degree line falling to the right because its x axis is inverted automatically. It appears that whether x axis is inverted or not depends on the values to be plotted.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df2 = pd.DataFrame(np.random.randn(10, 3), columns=["a", "b", "c"])
df3 = df2*1.1
df3.rename(columns={"a": "a*1.1", "b": "b*1.1", "c": "c*1.1"}, inplace=True)
df23 = df2.join(df3)
fig, ax_list = plt.subplots(1,3)
ax=ax_list[0]
df23[["a", "a*1.1"]].plot(ax=ax, x="a")
ax.axis('equal')
ax.set_title("(x,y)=(a,a*1.1)")
print ax.get_xlim() ## Added for clarity
ax=ax_list[1]
df23[["b", "b*1.1"]].plot(ax=ax, x="b")
ax.axis('equal')
ax.set_title("(x,y)=(b,b*1.1)")
print ax.get_xlim() ## Added for clarity
ax=ax_list[2]
df23[["c", "c*1.1"]].plot(ax=ax, x="c")
ax.axis('equal')
ax.set_title("(x,y)=(c,c*1.1)")
print ax.get_xlim() ## Added for clarity
推荐答案
我在熊猫的问题跟踪器,并得到了答案.
daraframe.plot(..) 是这样设计的
daraframe.plot(..) is designed such that
- 点的 x 坐标根据 x 参数指定的列的索引(即行号)确定.
- 点的y坐标是y参数指定的列的值.
对于散点图,我觉得上面的设计不太合适.我能想到的唯一解决方案是使用 plt.plot direclty.
For a scatter plot, I think the above design is not suitable. The only solution I can think of is to use plt.plot direclty.
cphlewis 的解决方法也很有用.
这篇关于x 轴被 pandas.plot(...) 意外反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!