python 如何在matplotlib中切换轴?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2361872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:27:49  来源:igfitidea点击:

How to switch axes in matplotlib?

pythonmatplotlib

提问by Daehyok Shin

I like to switch x axis with y axis after plotting a graph with matplotlib? Any easy way for it? Thanks in advance.

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

回答by raphael

I guess this would be a way to do it in case you really want to change data of an existing plot:

我想如果您真的想更改现有图的数据,这将是一种方法:

execute this code first to get a figure:

首先执行此代码以获取图形:

# 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])

and then use this to switch the data of an existing line

然后使用它来切换现有行的数据

# 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)

回答by G?khan Sever

You can simply switch x and y parameters in the plot function:

您可以简单地在 plot 函数中切换 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)