简体   繁体   中英

How to plot dataframe values on x-axis and indices on y-axis

I have two problems, but solving either of them neglects the need to solve the other.

I have a dataframe with temperature-values from eg 6 places in several (eg 3) heights. The heights are locked and are thus the indices of the dataframe. Example (does not match the figures):

  place1 place2 place3 place4 place5 place6
1     12     10      9     13     10      4
2     18     15     13     14      8      8
3     21      9     16     15     11     12

I want to plot the temperatures for every place as a function of the height, HOWEVER!, I want the height (the indices) to be along the Y-AXIS instead of the x-axis . So instead of having the graphs running 'from left to right', I want the to run 'from down to up'.

If I just Plot a DataFrame it looks somewhat like this: The indices (heights) run along the X-axis and not the Y-axis. - NOTE that values don't match table!!

ax.plot(data)

It does NOT work to transpose the array: it is still the values along the Y-axis that are flexible to locked X-values.

I want it to look like this with the variable temperature along the X-axis and the locked heights along the Y-axis. (PLEASE DON'T BOTHER the different dataset; the point is that the graphs are 'vertical' instead of horizontal):

ax.errorbar(temp, H, xerr=tempError, ecolor='gray')

The latter was made from plotting simple python lists and that work for me. However, my dataset is big so utilizing DataFrames would be nice AND, most importantly, I want to use the fill_betweenx to show the std instead of the errorbars currently presented as in the figure.

So I'm actually looking for an answer to any of these two questions:

  1. How do i flip the plot from a DataFrame (ie not transpose the df, but make the axis swap)?
  2. or, how do I utilize the fill_betweenx without using DataFrames?

Use pyplot.plot and structure the x's and y's the way you want.

from matplotlib import pyplot as plt
plt.plot(df.place1, df.index, df.place2, df.index, df.place3, df.index,
         df.place4, df.index, df.place5, df.index, df.place6, df.index)
plt.show()
plt.close()

Or a little more general:

for name, series in df.iteritems():
    plt.plot(series, df.index)
plt.show()
plt.close()

Here is suggestion for question no. 1

plt.plot(data, range(number_of_heights))

in your example number_of_heights is 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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