2016-08-07 15 views
1

Ich möchte bar und Linie zusammen in einem Diagramm darstellen. Wenn ich Balken plotten, zeigt er korrekt (G1 und G10 abgeschlossen sind angezeigt): enter image description hereMatplotlib Plot Bar und Liniendiagramme zusammen

Wenn ich jedoch eine Linie zu dem Grundstück hinzu:

m1_t[['abnormal','fix','normal']].plot(kind='bar') 
m1_t['bad_rate'].plot(secondary_y=True) 

Das Balkendiagramm ist unvollständig, wie unten (g1 und g10 sind gehackt):

Haben Sie eine Idee, wie Sie dieses Problem beheben können?

+0

Passen Sie einfach die xlimits der Handlung an – GWW

Antwort

1

Versuchen Sie, die Reihenfolge der Schalt Plotten:

ax = m1_t['bad_rate'].plot(secondary_y=True) 
m1_t[['abnormal','fix','normal']].plot(kind='bar', ax=ax) 

oder die Erhaltung der ursprünglichen barchart xlim:

ax = m1_t[['abnormal','fix','normal']].plot(kind='bar') 
m1_t['bad_rate'].plot(secondary_y=True, xlim=ax.get_xlim()) 
2

Sie haben zu erweitern x-Achse mit Xlim:

import matplotlib.pyplot as plt 
import numpy as np 
import pandas as pd 

width = .35 # width of a bar 

m1_t = pd.DataFrame({ 
'abnormal' : [90,40,30,30,30,25,25,20,15,10], 
'fix' : [60,70,65,70,70,60,50,45,45,45], 
'normal' : [140,160,170,180,190,200,210,220,230,240], 
'bad_rate' : [210,100,100,70,70,75,70,60,65,60]}) 

m1_t[['abnormal','fix','normal']].plot(kind='bar', width = width) 
m1_t['bad_rate'].plot(secondary_y=True) 

ax = plt.gca() 
plt.xlim([-width, len(m1_t['normal'])-width]) 
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'G10')) 

plt.show() 

enter image description here

Für zukünftige Fragen post Ihren Datenrahmen.