1.1.使用pyplot 的 hist() 函数可以快速绘制直方图,hist() 函数语法格式如
hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, label=None, stacked=False, normed=None, *,data=None, **kwargs)
import matplotlib.pyplot as plt
import numpy as np
#导入模块
random_state1 = np.random.RandomState(19680801)
random_x1 = random_state1.randn(10000)
random_state1 = np.random.RandomState(19680801)
random_x2 = random_state1.randn(20000)
#插入数据
plt.hist([random_x1,random_x2],bins=25,facecolor='r')
#作图
plt.show()
2.使用pyplot 的 pie() 函数可以快速绘制饼图或环形图,pie() 函数语法格式如下
pie(x, explode=None, labels=None, autopct=None, pctdistance=0.6, shaow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textpropos=None, center=(0, 0), frame=False, *, data=None)
import matplotlib.pyplot as plt
plt.rcParams['font.family']='SimHei'
x = [1, 5, 4, 3]
labels = ['a', 'b', 'c', 'd']
explode = [-0.1, 0, 0.1, 0]
colors =['r','b']
# 子图1,显示默认属性情况,为与子图2对比,添加了外标签
plt.subplot(241)
plt.pie(x, labels=labels)
plt.title('默认属性')
# 子图2,演示外标签相关属性
plt.subplot(242)
plt.pie(x, labels=labels,labeldistance=1.2,rotatelabels=True)
plt.title('外标签相关属性')
# 子图3,演示内标签相关属性,起始角度设置为30
plt.subplot(243)
plt.pie(x, autopct='%1.1f%%',pctdistance=0.4,startangle=30)
plt.title('内标签相关属性')
# 子图4,演示饼块分离、颜色循环、阴影
plt.subplot(244)
plt.pie(x, explode=explode, colors=colors,shadow=True)
plt.title('饼块分离、颜色循环')
# 子图5,演示修改饼图半径、角度顺时针旋转
plt.subplot(245)
plt.pie(x, radius=1.2,counterclock=False)
plt.title('修改半径、顺时针')
# 子图6,演示normalize为默认值None
plt.subplot(246)
x = [0.1, 0.2, 0.2, 0.3]
plt.pie(x)
# normalize为默认值None,这时sum(x)<1,相当于normalize=Fasle
# 运行时会有警告,因为未来版本默认normalize=True
# 建议绘制不完全饼图使用plt.pie(x,normalize=Fasle)
plt.title('normalize=None')
# 子图7,演示normalize为True,这时虽然sum(x)<1,但是会强制归一化,绘制完全饼图
plt.subplot(247)
x = [0.1, 0.2, 0.2, 0.3]
plt.pie(x,normalize=True)
plt.title('normalize=True')
# 子图8,演示normalize为False,这时sum(x)<1,绘制不完全饼图
plt.subplot(248)
x = [0.1, 0.2, 0.2, 0.3]
plt.pie(x,normalize=False)
plt.title('normalize=False')
plt.show()
2.1 饼图
import numpy as np
import matplotlib.pyplot as plt
data = np.array([20, 50, 10, 15, 30, 55])
pie_labels = np.array(['A', 'B', 'C', 'D', 'E', 'F'])
# 绘制饼图 :半径为0.5, 数值保留1位小数
plt.pie(data, radius=1.5, labels=pie_labels, autopct='%3.1f%%')
plt.show()
环形图示示例
#导入模块
import numpy as np
import matplotlib.
plt.pie(data, radius=1.5, labels=pie_labels, wedgeprops={'width': 0.7},
autopct='%3.1f%%', pctdistance=0.75)
#圆环图 :外圆半径为1.5, 楔形宽度为0.7
pyplot as plt
#插入数据
data = np.array([20, 50, 10, 15, 30, 55])
pie_labels = np.array(['A', 'B', 'C', 'D', 'E', 'F'])
# 绘制
plt.show()
实操代码
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
# 外侧说明文字
kinds = ['购物', '人情往来', '餐饮美食', '通信物流', '生活日用', '交通出行', '休闲娱乐', '其他']
# 数据
money_scale = [1000, 100, 800, 200, 300, 200, 20, 200]
# 爆炸程度
dev_position = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
plt.pie(money_scale, labels=kinds, wedgeprops={'width':0.7},autopct='%3.1f%%',pctdistance=0.75,shadow=True,explode=dev_position,startangle=90,radius=1.5)
plt.show()