Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot Pandas DataFrame as Bar and Line on the same one chart

Tags:

I am trying to plot a chart with the 1st and 2nd columns of data as bars and then a line overlay for the 3rd column of data.

I have tried the following code but this creates 2 separate charts but I would like this all on one chart.

left_2013 = pd.DataFrame({'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],                      '2013_val': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 6]})  right_2014 = pd.DataFrame({'month': ['jan', 'feb'], '2014_val': [4, 5]})  right_2014_target = pd.DataFrame({'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],                                    '2014_target_val': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]})   df_13_14 = pd.merge(left_2013, right_2014, how='outer') df_13_14_target = pd.merge(df_13_14, right_2014_target, how='outer') df_13_14_target[['month','2013_val','2014_val','2014_target_val']].head(12)  plt.figure() df_13_14_target[['month','2014_target_val']].plot(x='month',linestyle='-', marker='o') df_13_14_target[['month','2013_val','2014_val']].plot(x='month', kind='bar') 

This is what I currently get

kpi dashboard

like image 379
tfayyaz Avatar asked May 05 '14 21:05

tfayyaz


People also ask

What is the method of data frame to plot a line chart?

line() function is used to plot series or DataFrame as lines. This function is useful to plot lines using DataFrame's values as coordinates. Columns to use for the horizontal axis.

How do you make a bar graph with two sets of data in Python?

arange( ) function from numpy library is used to create a range of values. Plotting the multiple bars using plt. bar( ) function in matplotlib library. To avoid overlapping of bars in each group, the bars are shifted 0.25 units from the X-axis in this example.


1 Answers

The DataFrame plotting methods return a matplotlib AxesSubplot or list of AxesSubplots. (See the docs for plot, or boxplot, for instance.)

You can then pass that same Axes to the next plotting method (using ax=ax) to draw on the same axes:

ax = df_13_14_target[['month','2014_target_val']].plot(x='month',linestyle='-', marker='o') df_13_14_target[['month','2013_val','2014_val']].plot(x='month', kind='bar',     ax=ax) 

import pandas as pd import matplotlib.pyplot as plt  left_2013 = pd.DataFrame(     {'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep',                'oct', 'nov', 'dec'],      '2013_val': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 6]})  right_2014 = pd.DataFrame({'month': ['jan', 'feb'], '2014_val': [4, 5]})  right_2014_target = pd.DataFrame(     {'month': ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep',                'oct', 'nov', 'dec'],      '2014_target_val': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]})  df_13_14 = pd.merge(left_2013, right_2014, how='outer') df_13_14_target = pd.merge(df_13_14, right_2014_target, how='outer')  ax = df_13_14_target[['month', '2014_target_val']].plot(     x='month', linestyle='-', marker='o') df_13_14_target[['month', '2013_val', '2014_val']].plot(x='month', kind='bar',                                                         ax=ax)  plt.show() 

enter image description here

like image 84
unutbu Avatar answered Oct 15 '22 18:10

unutbu