绘制饼状图
import matplotlib.pyplot as plt
edu = ['0.2515','0.3724','0.3336','0.0368','0.0057']
labels = ['中专','大专','本科','硕士','其他']
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']#使图像中可以显示中文
explode =[0,0,0,0.1,0]#将硕士块突出,拉离中心0.1
plt.pie(x=edu,explode = explode,labels = labels,autopct='%.2f%%')
plt.show()
绘制柱状图
1、绘制普通柱状图
import pandas as pd
GDP = pd.read_csv('/Users/eileen/Documents/PYlearn/datas/GDP.txt',sep=' ')
GDP = GDP.drop(labels=['ID','Unnamed: 3'],axis =1).rename(columns = {
'GDP(亿元)':'GDP','省份':'province'})
GDP.GDP = round(GDP.GDP/10000,1)
plt.style.use('ggplot')#增加绘图风格
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.bar(x=range(GDP.shape[0]),height = GDP.GDP,tick_label = GDP.province, color = 'steelblue')
#tick_label将横坐标替换为中文标签
plt.ylabel('GDP(亿万元)')#添加y轴标签
plt.title('全国前十二GDP排行')#添加图像标题
for x,y in enumerate(GDP.GDP):
plt.text(x,y+0.1,'%s' %y,ha='center')#为图像添加文本
plt.show()
2、对数据排序
DataFrame.sort_values(by=‘ ’,axis=0,ascending=True, inplace=False, na_position=‘last’)
参数 | 说明 |
---|---|
by | 指定列名(axis=0或’index’)或索引值(axis=1或’columns’) |
axis | 若axis=0或’index’,则按照指定列中数据大小排序;若axis=1或’columns’,则按照指定索引中数据大小排序,默认axis=0 |
ascending | 是否按指定列的数组升序排列,默认为True,即升序排列 |
inplace | 是否用排序后的数据集替换原来的数据,默认为False,即不替换 |
na_position | {‘first’,‘last’},设定缺失值的显示位置 |
3、水平柱状图
GDP.sort_values(by= 'GDP',ascending = False,inplace = True)
plt.barh(y=range(GDP.shape[0]), width = GDP.GDP, tick_label = GDP.province, color = 'indianred')
plt.xlabel('GDP(亿万元)')
plt