matplotlib Drawing Tutorial Series - stacking bar

matplotlib stacking bar painting Introduction

Stacked Column can be understood in two histogram displays a graph, with the bottom key attributes in this attribute.

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('planets.csv')
print(df.head(10))
df.fillna(value={'mass':df.mass.mean()},inplace=True)
temp = pd.pivot_table(data=df,index='method',columns='number',values='mass',aggfunc=np.sum)
print(temp)
temp.fillna(0,inplace=True)
print(temp.columns.values)#获取dataframe 的x方向的属性值
print(temp.loc['Radial Velocity',:])
plt.rcParams['font.sans-serif']=['SimHei']#这两句作用为防止中文乱码
plt.rcParams['axes.unicode_minus']=False
plt.bar(x = temp.columns.values,height=temp.loc['Radial Velocity',:],color='steelblue',label='Astrometry',\
        tick_label=['数量1','数量2','数量3','数量4','数量5','数量6','数量7'],)
plt.bar(x=temp.columns.values,height=temp.loc['Transit',:],color='red',label='Transit',\
        tick_label=['数量1','数量2','数量3','数量4','数量5','数量6','数量7'],bottom=temp.loc['Radial Velocity',:])
plt.legend(loc='best')
plt.show()

Directly see the results chart:

Here Insert Picture Description

On how to draw a histogram, I will not go into here, I want to know the students can see what I wrote before the draw for the introduction of the histogram matplotlib drawing tutorial series - bar chart

Here just talk about the bottom of this property, which is the meaning of what values ​​were drawing up as a starting point.

Let's look at the second bar method, bottom = temp.loc [ 'Radial Velocity ' ,:]
This is the value of the first bottom bar method, height = temp.loc [ 'Radial Velocity ' ,:], the value. Of course, these two methods to ensure that the bar with the same x-coordinate axis, i.e. the value of x to the same property.

Published 67 original articles · won praise 54 · Views 230,000 +

Guess you like

Origin blog.csdn.net/lzx159951/article/details/104391873