'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.
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.
Solution 1:[1]
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:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | trsvchn |


