2017-04-22 9 views
0

Datensatz:Overlap der Gruppe BarCharts in matplotlib

enter image description here

-Code zu zeichnen:

import numpy as np 
import matplotlib.pyplot as plt 

# data to plot 
n_groups = 6 
GreenTaxi = pivot_df['GreenTaxi'].tolist() 
YellowTaxi = pivot_df['YellowTaxi'].tolist() 
Uber = pivot_df['Uber'].tolist() 

# create plot 
fig, ax = plt.subplots() 
index = np.arange(n_groups) 
bar_width = 0.35 
opacity = 0.8 

rects1 = plt.bar(index, GreenTaxi, bar_width, 
       alpha=opacity, 
       color='b', 
       label='GreenTaxi') 

rects2 = plt.bar(index + bar_width, YellowTaxi, bar_width, 
       alpha=opacity, 
       color='g', 
       label='YellowTaxi') 

rects3 = plt.bar(index + bar_width, Uber, bar_width, 
       alpha=opacity, 
       color='r', 
       label='Uber') 

plt.xlabel('Month') 
plt.ylabel('Ride Counts') 
plt.title('Rides By Month') 
plt.figure(figsize=(5,5)) 
plt.xticks(index + bar_width,pivot_df['MonthName'].tolist()) 
plt.legend() 
plt.show() 

Ausgang:

enter image description here

Die grünen und roten Balkendiagramme sind überlappende und mehr über MonthName kommt nicht in die x Label, nur Zahlen kommen. Könnten Sie bitte auf die Frage hinweisen?

Antwort

0

Ich glaube, Sie durch Teilmenge DataFrame.plot.bar, auch erste set_index mit Spalte Names und dann Reihenfolge der Spalten ändern [] verwenden können:

bar_width = 0.35 
opacity = 0.8 
colors = ['b','g','r'] 
cols = ['GreenTaxi','YellowTaxi','Uber'] 
title = 'Rides By Month' 
ylabel = "Ride Counts" 

ax = pivot_df.set_index('MonthName')[cols].plot.bar(width=bar_width, 
                alpha=opacity, 
                figsize=(5,5), 
                color=colors, 
                title=title) 
ax.set_ylabel(ylabel) 
ax.legend(loc='best',prop={'size':10}) 

graph

0
import pandas as pd 
import numpy as np 

df=pd.DataFrame(10*np.random.rand(4,3),index=["Jan","Feb","March","April"],columns=["Uber","T1", "T2"]) 

df.plot(kind='bar',rot=0) 

enter image description here