python matplotlib.pyplot 画图

1.matplotlib.pyplot柱状图

import matplotlib.pyplot as plt 
labels_count = [41,32,28,22,22,21,21,21,20,19]
labels_name = ["上海理赔管理小组","上海嘉定区外冈五金城营业部","上海浦东保税区营业部","上海闵行区银都路营业部",\
               "上海徐汇区凯旋路营业部","上海浦东张江高科营业部","上海松江永丰营业部","上海闸北区灵石路营业部",\
               "上海嘉定区徐行营业部","上海奉贤区金汇镇金钱公路营业部"]
plt.figure()
plt.bar(labels_name,labels_count,color='pink')
plt.show()

python matplotlib.pyplot 画图_第1张图片
2.问题1:柱状图没有按给定顺序排列
解决方案:使用plt.xticks()方法人为修改x轴显示顺序,plt.bar函数中不要直接用x数组,使用arange()函数生成x轴刻度
问题2:横轴文字叠加在一起
解决方案:将横轴文字字体变小
问题3:中文显示乱码
解决方案:加一句代码

plt.rcParams['font.sans-serif']=['SimHei'] #添加中文
import matplotlib.pyplot as plt 
labels_count = [41,32,28,22,22,21,21,21,20,19]
labels_name = ["上海理赔管理小组","上海嘉定区外冈五金城营业部","上海浦东保税区营业部","上海闵行区银都路营业部",\
               "上海徐汇区凯旋路营业部","上海浦东张江高科营业部","上海松江永丰营业部","上海闸北区灵石路营业部",\
               "上海嘉定区徐行营业部","上海奉贤区金汇镇金钱公路营业部"]
plt.figure()
#plt.bar(labels_name,labels_count,color='skyblue')
plt.bar(range(len(labels_name)),labels_count,color='skyblue')
plt.xticks(range(len(labels_name)), labels_name,fontsize = 7)
plt.show()

python matplotlib.pyplot 画图_第2张图片
3.给柱状图加上纵横轴标签,标题,并显示柱状图对应数值,旋转横坐标文字

import matplotlib.pyplot as plt 
labels_count = [41,32,28,22,22,21,21,21,20,19]
labels_name = ["上海理赔管理小组","上海嘉定区外冈五金城营业部","上海浦东保税区营业部","上海闵行区银都路营业部",\
               "上海徐汇区凯旋路营业部","上海浦东张江高科营业部","上海松江永丰营业部","上海闸北区灵石路营业部",\
               "上海嘉定区徐行营业部","上海奉贤区金汇镇金钱公路营业部"]
plt.rcParams['font.sans-serif']=['SimHei'] #添加中文
plt.figure()
plt.bar(range(len(labels_name)),labels_count,color='skyblue')
plt.xticks(range(len(labels_name)), labels_name,fontsize = 7)
plt.xticks(rotation=345) 
plt.xlabel("网点名称")
plt.ylabel("个数")
plt.title("各网点有效营销个数")
for a,b in zip(range(len(labels_name)),labels_count):
    plt.text(a, b+0.05, '%.0f' % b, ha='center', va= 'bottom',fontsize=12)
plt.show()

python matplotlib.pyplot 画图_第3张图片
python matplotlib.pyplot 画图_第4张图片
4.matplotlib.pyplot条状图

import matplotlib.pyplot as plt 
labels_count = [41,32,28,22,22,21,21,21,20,19]
labels_name = ["上海理赔管理小组","上海嘉定区外冈五金城营业部","上海浦东保税区营业部","上海闵行区银都路营业部",\
               "上海徐汇区凯旋路营业部","上海浦东张江高科营业部","上海松江永丰营业部","上海闸北区灵石路营业部",\
               "上海嘉定区徐行营业部","上海奉贤区金汇镇金钱公路营业部"]
import matplotlib.pyplot as plt 
plt.rcParams['font.sans-serif']=['SimHei'] 
plt.figure()
plt.barh(range(len(labels_name)),labels_count,tick_label = labels_name,color='skyblue')
plt.title("各网点有效营销个数")
for a,b in zip(range(len(labels_name)),labels_count):
    plt.text(b+1,a-0.2,  '%.0f' % b, ha='center', va= 'bottom',fontsize=12)
plt.show()   

python matplotlib.pyplot 画图_第5张图片
5.matplotlib.pyplot饼状图

import matplotlib.pyplot as plt 
labels_count = [41,32,28,22,22,21,21,21,20,19]
labels_name = ["上海理赔管理小组","上海嘉定区外冈五金城营业部","上海浦东保税区营业部","上海闵行区银都路营业部",\
               "上海徐汇区凯旋路营业部","上海浦东张江高科营业部","上海松江永丰营业部","上海闸北区灵石路营业部",\
               "上海嘉定区徐行营业部","上海奉贤区金汇镇金钱公路营业部"]
plt.rcParams['font.sans-serif']=['SimHei'] #添加中文
plt.figure(figsize=(6,9)) #调节图形大小
colors = ['red','yellowgreen','lightskyblue','pink'] #每块颜色定义
explode = (0,0,0.02,0) #将某一块分割出来,值越大分割出的间隙越大
patches,text1,text2 = plt.pie(labels_count[:4],
                       explode=explode,
                       labels=labels_name[:4],
                       colors=colors,
                       labeldistance = 1.2,#图例距圆心半径倍距离
                       autopct = '%3.2f%%', #数值保留固定小数位
                       shadow = False, #无阴影设置
                       startangle =90, #逆时针起始角度设置
                       pctdistance = 0.6) #数值距圆心半径倍数距离
#patches饼图的返回值,texts1饼图外label的文本,texts2饼图内部文本
# x,y轴刻度设置一致,保证饼图为圆形
plt.axis('equal')
plt.legend()
plt.show()    

python matplotlib.pyplot 画图_第6张图片

你可能感兴趣的:(python)