matplotlib 交换 x 和 y 轴 [英] matplotlib swap x and y axis

查看:127
本文介绍了matplotlib 交换 x 和 y 轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经使用熊猫在matplot lib中进行了绘制,但是我需要交换x和y轴.

这是我的情节:

Hello I have made a plot in matplot lib using pandas however I need to swap my x and y axis.

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. Here some of my python code:

python code

Any resources or ideas would be greatly appreciated.

解决方案

Try this. You need to include your y_vals as an additional column. So with this you can just specify your axis here 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:

这篇关于matplotlib 交换 x 和 y 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆