python - Plot bar graph and timeseries plot on different axis using pandas -


i have pandas data frame read in .csv file structure:

date,       latitude,   longitude,        brand,        pump, aki,  trip miles,  total miles, gallons,  mpg,    ppg,    total,  tires,  mpg-d, 11/03/2013, 40° 1.729', -105° 15.516',    boulder gas,  2,    87,   134.3,       134.3,       6.563,    20.46,  3.319,  21.78,  stock,  , 11/17/2013, 40° 1.729', -105° 15.516',    boulder gas,  2,    87,   161.8,       296.0,       7.467,    21.67,  3.279,  24.48,  stock,  , 11/27/2013, 40° 0.872', -105° 12.775',    buffalo gas,  6,    87,   180.8,       477.0,       8.096,    22.33,  3.359,  27.19,  stock,  , 12/07/2013, 40° 1.729', -105° 15.516',    boulder gas,  6,    87,   265.1,       742.0,       12.073,   21.96,  3.179,  38.38,  stock,  , 12/11/2013, 40° 2.170', -105° 15.522',    circle k,     4,    87,   240.9,       983.0,       9.868,    24.41,  3.179,  31.37,  stock,  , 12/15/2013, 40° 8.995', -105° 7.876',     shell,        3,    87,   188.7,       1172,        8.596,    21.95,  3.059,  26.30,  ,       , 12/21/2013, 40° 1.770', -105° 15.481',    conoco,       3,    87,   113.8,       1286,        5.517,    20.62,  3.139,  17.32,  winter, , 01/09/2014, 40° 1.729', -105° 15.516',    boulder gas,  2,    87,   139.5,       1426,        7.181,    19.42,  3.279,  23.55,  winter, 21.3, 01/13/2013, 40° 1.770', -105° 15.481',    conoco,       7,    87,   260.8,       1688,        11.177,   23.33,  3.239,  36.20,  winter, 25.5, 01/18/2014, 40° 1.729', -105° 15.516',    boulder gas,  2,    87,   102.0,       1790,        4.401,    23.18,  3.239,  14.26,  winter, 25.5, 02/02/2014, 39° 59.132', -105° 14.962',   king soopers, 5,    87,   175.3,       1965,        8.436,    20.78,  3.019,  25.47,  winter, 24.0, 02/03/2014, 40° 1.770', -105° 15.481',    conoco,       3,    87,   249.9,       2215,        10.452,   23.91,  3.219,  33.64,  winter, 25.2, 02/08/2014, 40° 2.170', -105° 15.522',    circle k,     7,    87,   186.4,       2402,        8.565,    21.76,  3.239,  27.74,  winter, 24.3, 02/13/2014, 40° 1.729', -105° 15.516',    boulder gas,  8,    87,    79.6,       2481,        4.125,    19.30,  3.439,  14.19,  winter, 21.3, 03/06/2014, 40.014460, -105.225034,       conoco,       5,    87,   172.4,       2654,        8.618,    20.00,  3.779,  32.57,  winter, 21.9, 03/09/2014, 40.029498, -105.258117,       conoco,       6,    87,   230.4,       2884,        9.016,    25.55,  3.759,  33.89,  winter, 27.3, 03/17/2014, 40.036236, -105.258763,       conoco,       6,    87,   130.1,       3014,        5.368,    24.24,  3.719,  19.96,  winter, 25.8, 03/24/2014, 40.036236, -105.258763,       conoco,       1,    87,   282.3,       3297,       11.540,    24.46,  3.719,  42.92,  winter, 27.3, 

i want produce plot x-axis date, left y-axis miles/gallon, , right y-axis miles. in plot want show time series 'mpg' column in 1 color, time series 'mpg-d' in color , bar graph of 'trip miles' column in third color.

i have been trying follow http://pandas.pydata.org/pandas-docs/stable/visualization.html , have code below produces bar plot , both time series plots on same axis , y-label not shown.

%matplotlib inline import pandas pd import matplotlib.pyplot plt  data = pd.read_csv('mpg.csv', skipinitialspace=true,index_col='date') plt.figure() ax = data['trip miles'].plot(kind='bar',secondary_y=['trip miles']) ax.right_ax.set_ylabel('miles') ax.set_ylabel('miles/gallon') data['mpg'].plot() data['mpg-d'].plot() 

plot above code

you need specify axes more explicitly. try this:

%matplotlib inline import pandas pd import matplotlib.pyplot plt  fig, tsax = plt.subplots() barax = tsax.twinx()  data = pd.read_csv('mpg.csv', skipinitialspace=true,index_col='date') data['trip miles'].plot(kind='bar', ax=barax) barax.set_ylabel('miles') tsax.set_ylabel('miles/gallon') data['mpg'].plot(ax=tsax) data['mpg-d'].plot(ax=tsax) 

edit

so big problem here pandas bar plots , line plots format x-axis in fundamentally different ways. specifically, bar plots attempt make qualitative scales ticks , labels every single bar. seems here you're interested in getting format more typical time series.

so here's suggest forget dual axis charts. instead, plot on 2 separate axes. this:

import numpy np import matplotlib.pyplot plt import matplotlib.gridspec mgrid import pandas pd  fig = plt.figure(figsize=(12,5)) grid = mgrid.gridspec(nrows=2, ncols=1, height_ratios=[2, 1])  barax = fig.add_subplot(grid[0]) tsax = fig.add_subplot(grid[1]) data = pd.dataframe(np.random.randn(10,3), columns=list('abc'), index=pd.datetimeindex(freq='1m', start='2012-01-01', periods=10))  data['a'] **= 2 data['a'].plot(ax=barax, style='o--') barax.set_ylabel('miles') tsax.set_ylabel('miles/gallon')  barax.xaxis.tick_top()  data['b'].plot(ax=tsax) data['c'].plot(ax=tsax) fig.tight_layout() 

which gives me: separate axes

however, if really need bars or really want on same twin x-axes, have plot matplotlib's api this:

import numpy np import matplotlib.pyplot plt import matplotlib.gridspec mgrid import pandas pd  fig, tsax = plt.subplots(figsize=(12,5)) barax = tsax.twinx()  data = pd.dataframe(np.random.randn(10,3), columns=list('abc'), index=pd.datetimeindex(freq='1m', start='2012-01-01', periods=10)) data['a'] **= 2  # `width` specified in days -- adjust data barax.bar(data.index, data['a'], width=5, facecolor='indianred')  barax.set_ylabel('miles') tsax.set_ylabel('miles/gallon')  barax.xaxis.tick_top()  fig.tight_layout()  tsax.plot(data.index, data['b']) tsax.plot(data.index, data['c']) 

which gives me

single axes


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -