matplotlib
一、绘制饼图
1.直接生成饼图
import matplotlib.pyplot as plt
edu = [0.2515,0.3724,0.3336,0.0368,0.0057]
labels = ['中专','大专','本科','硕士','其他']
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
colors=['#9999ff','#ff9999','#7777aa','#2442aa','#dd5555']
plt.rcParams['axes.unicode_minus'] = False
plt.axes(aspect='equal')
plt.pie(x = edu,
explode=explode,
labels=labels,
colors=colors,
autopct='%.1f%%',
pctdistance=0.8,
labeldistance=1.1,
startangle=180,
radius=1.2,
counterclock=False,
wedgeprops={
'linewidth':1.5,'edgecolor':'green'},
textprops={
'fontsize':10,'color':'black'},
)
plt.title('失信用户的教育水平')
plt.show()
2.用数组生成饼图
import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
data1 = pd.Series({
'中专':0.2515,'大专':0.3724,'本科':0.3336,'硕士':0.0368,'其他':0.0057})
data1.name = ''
plt.axes(aspect = 'equal')
data1.plot(kind = 'pie',
autopct='%.1f%%',
radius = 1,
startangle = 180,
counterclock = False,
title = '失信用户的受教育水平分布',
wedgeprops = {
'linewidth':1.5,'edgecolor':'green'},
textprops = {
'fontsize':10,'color':'black'}
)
plt.show()
二、绘制条形图
1.绘制垂直条形图
GDP = pd.read_excel(r'E:\Province GDP 2017.xlsx')
plt.style.use('ggplot')
plt.bar(x = range(GDP.shape[0]),
height = GDP.GDP,
tick_label = GDP.Province,
color = 'steelblue',
)
plt.ylabel('GDP(万亿)')
plt.title('2017年度6个省份GDP分布')
for x,y in enumerate(GDP.GDP):
plt.text(x,y+0.1,'%s' %round(y,1),ha='center')
plt.show()
2.绘制水平条形图
GDP.sort_values(by = 'GDP',inplace = True)
plt.barh(y = range(GDP.shape[0]),
width = GDP.GDP,
tick_label = GDP.Province,
color = 'steelblue',
)
plt.xlabel('GDP(万亿)')
plt.title('2017年度6个省份GDP分布')
for x,y in enumerate(GDP.GDP):
plt.text(x+0.1,y,'%s' %round(x,1),va='center')
plt.show()
3.绘制堆叠条形图
堆叠条形图原理:用好几段水平or垂直条形图,分别用不同的颜色
import pandas as pd
import matplotlib.pyplot as plt
Industry_GDP = pd.read_excel(r