I have a data frame with 60 columns. I want the columns to be plotted on the X axis, and the index to be plotted on the y-axis.
df.plot()
The above code puts the index on the x-axis by default. I am unable to work out how to switch the axes. Any help appreciated.
You can use yourDataFrame. plot(use_index=True) to use the DataFrame Index On X-Axis. The "use_index=True" sets the DataFrame Index on the X-Axis.
To create an index, from a column, in Pandas dataframe you use the set_index() method. For example, if you want the column “Year” to be index you type <code>df. set_index(“Year”)</code>. Now, the set_index() method will return the modified dataframe as a result.
You can set the labels on that object. Or, more succinctly: ax. set(xlabel="x label", ylabel="y label") . Alternatively, the index x-axis label is automatically set to the Index name, if it has one.
Maybe you can plot the DataFrame transposed.
Creating a DataFrame with 10 columns and 5 rows.
In [1]: import pandas as pd In [2]: import numpy as np In [3]: df = pd.DataFrame(np.random.rand(5,10)) In [4]: df Out[4]: 0 1 2 3 4 5 6 \ 0 0.221466 0.375541 0.318416 0.695447 0.117332 0.836268 0.326673 1 0.439128 0.546087 0.380608 0.599192 0.553365 0.739253 0.984701 2 0.189311 0.799067 0.322041 0.171035 0.595848 0.106069 0.940712 3 0.971887 0.416420 0.945873 0.828611 0.055950 0.690107 0.526104 4 0.643041 0.800376 0.010879 0.677169 0.268208 0.151503 0.062268 7 8 9 0 0.690805 0.741308 0.487934 1 0.827711 0.284417 0.878259 2 0.734911 0.597626 0.256107 3 0.583156 0.398342 0.406590 4 0.247892 0.990469 0.611637 In [8]: df.plot()
In [9]: df.T.plot()
The following are the contents of the DataFrame transposed.
In [10]: df.T Out[10]: 0 1 2 3 4 0 0.221466 0.439128 0.189311 0.971887 0.643041 1 0.375541 0.546087 0.799067 0.416420 0.800376 2 0.318416 0.380608 0.322041 0.945873 0.010879 3 0.695447 0.599192 0.171035 0.828611 0.677169 4 0.117332 0.553365 0.595848 0.055950 0.268208 5 0.836268 0.739253 0.106069 0.690107 0.151503 6 0.326673 0.984701 0.940712 0.526104 0.062268 7 0.690805 0.827711 0.734911 0.583156 0.247892 8 0.741308 0.284417 0.597626 0.398342 0.990469 9 0.487934 0.878259 0.256107 0.406590 0.611637
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With