2016-08-07 50 views
1

我想在一個圖表繪製欄和行在一起。當我繪製條,它顯示正確(g1和G10被顯示完成):enter image description herematplotlib陰謀條形圖和折線圖一起

但是,如果我添加一行的情節:

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

條形圖是不完整的,如下(g1和g10切碎): enter image description here

任何想法如何解決這個問題?

+0

只需調整劇情的xlimits – GWW

回答

1

嘗試切換繪製的順序:

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

或保留原來的條形圖xlim

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

你要擴大x軸與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

對於將來的問題發佈您的數據框。