简体   繁体   English

matplotlib 交换 x 和 y 轴

[英]matplotlib swap x and y axis

Hello I have made a plot in matplot lib using pandas however I need to swap my x and y axis.您好,我已经使用 pandas 在 matplot lib 中绘制了一个图,但是我需要交换我的 x 和 y 轴。

Here is my plot:这是我的情节:

broomstick plot扫帚情节
扫帚情节

however i need it to look like this:但是我需要它看起来像这样:

correct broomstick plot正确的扫帚图
正确的扫帚图

I'm using a pandas dataframe to plot the data.我正在使用熊猫数据框来绘制数据。

I've looked over some documentation and other posts regarding swapping the x and y axis and haven't found any easy way to do this.我查看了一些关于交换 x 和 y 轴的文档和其他帖子,但没有找到任何简单的方法来做到这一点。 Here some of my python code:这是我的一些python代码:

python code蟒蛇代码
蟒蛇代码

Any resources or ideas would be greatly appreciated.任何资源或想法将不胜感激。

Try this.尝试这个。 You need to include your y_vals as an additional column.您需要将y_vals作为附加列包含在内。 So with this you can just specify your axis here df.plot(x=, y=) :所以有了这个你可以在这里指定你的轴df.plot(x=, y=)

Code:代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


x = np.linspace(0, 10, 100)
y = np.cos(x)

df = pd.DataFrame({'y': y, 'x': x})

df.plot(x='x')
plt.show()

df.plot(x='y')
plt.show()

Plots:情节:

在此处输入图像描述 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM