gpt4 book ai didi

python - pandas.plot(...) 意外反转 x 轴

转载 作者:太空宇宙 更新时间:2023-11-04 08:06:44 26 4
gpt4 key购买 nike

当使用 pandas 将一系列数据与另一系列数据进行对比时,X 轴意外地自动反转。请看下面我的代码。如何确保 x 轴始终指向右侧? x 轴的这种自动反转是 Pandas 的预期行为吗?可以禁用吗?

让我在下面解释我的例子。创建了三个地 block 。我希望每一个都显示向右上升的近 45 度线。然而,其中一些有 45 度线向右倾斜,因为它的 x 轴自动反转。似乎 x 轴是否反转取决于要绘制的值。

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

Created plot

最佳答案

我在 issue tracker of pandas 上问过这个问题, 并得到答复。

daraframe.plot(..) 的设计使得

  • 点的x坐标根据x参数指定的列的索引(即行号)确定。
  • 点的y坐标为y参数指定的列的值。

对于散点图,我觉得上面的设计不太合适。我能想到的唯一解决方案是直接使用 plt.plot。

cphlewis's workaround也很有用。

关于python - pandas.plot(...) 意外反转 x 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908874/

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