【问题标题】:How to switch axes in matplotlib?如何在 matplotlib 中切换轴?
【发布时间】:2011-01-22 15:25:55
【问题描述】:

我喜欢在用 matplotlib 绘制图形后切换 x 轴和 y 轴?有什么简单的方法吗?提前致谢。

【问题讨论】:

标签: python matplotlib


【解决方案1】:

我想这将是一种方法,以防你真的 想要更改现有地块的数据:

先执行这段代码得到一个图:

# generate a simple figure
f, ax = plt.subplots()
ax.plot([1,2,3,4,5], [5,6,7,8,9])
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])

然后用这个来切换已有行的数据

# get data from first line of the plot
newx = ax.lines[0].get_ydata()
newy = ax.lines[0].get_xdata()

# set new x- and y- data for the line
ax.lines[0].set_xdata(newx)
ax.lines[0].set_ydata(newy)

【讨论】:

  • 这个解决方案引用的第一行是什么?是不是表示如果有 >=2 行就会失败?
  • 如果你想切换多行,只需将它放在一个循环中! (例如for line in ax.lines: ...
  • 仅当轴具有相同的 dtype 时才有效!
【解决方案2】:

您可以在绘图函数中简单地切换 x 和 y 参数:

I[3]: x = np.linspace(0,2*np.pi, 100)

I[4]: y = np.sin(x)

I[5]: plt.plot(x,y)

I[6]: plt.figure(); plt.plot(y,x)

【讨论】:

  • 谢谢,但这不是我想要的答案。我在问如何在绘制完所有数据后切换 x 和 y 轴。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多