#绘制饼状图
import matplotlib.pyplot as plt
import numpy as np
man=2245505
woman=3254584
man_prec=man/(man+woman)
woman_prec=woman/(man+woman)
labels=['男','女']
color=['blue','red']
plt.pie([man_prec,woman_prec],labels=labels,colors=color,autopct='%0.1f%%')
plt.rcParams['font.sans-serif']=['SimHei']
plt.show()
#绘制散点图
import matplotlib.pyplot as plt
import numpy as np
'''x=np.linspace(1,10,100)
sin_y=np.sin(x)
#plt.plot(x,sin_y,'o')
plt.scatter(x,sin_y)
plt.show()
如果画一堆形式不差别的点,plt的选择优于scatter,当点的大小和颜色有差别时,使用scatter'''
#不同的散点图
np.random.seed(0)
x=np.random.rand(100)
y=np.random.rand(100)
# size=np.random.rand(100)*100
# color=np.random.rand(100)
plt.scatter(x,y)#s代表大小,c代表颜色,alpha代表透明度
plt.show()
#绘制柱状图
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from matplotlib import rcParams
import numpy as np
# 创建x,y,其中x表示年份,y表示数值
x=[2000,2001,2002,2003,2004,2005]
x_label=['2000年','2001年','2002年','2003年','2004年','2005年']
y=[3434,3455,5964,2556,5645,5456]
index = np.arange(6)*2.5 ###设置索引,控制不同柱之间的距离用于后面的画柱状图
#调用bar函数绘制柱状图
plt.figure(figsize=(6,10)) #改变画布大小
plt.twinx()
# ls:线的类型,lw:宽度,o:在顶点处实心圈
plt.plot(x, y, ls='-',lw=2,color='#59b3b3',marker='o',label='目标完成率')
plt.bar(x,y,width=0.5,yerr =500,error_kw = {'ecolor' : '0.2', 'capsize' :6 }, alpha=0.7,color='#b35959' ,label = 'XXXX')
plt.xticks(x, x_label)
plt.xlabel("年份")
plt.ylabel('销量')
plt.title("年份销量柱状图")
plt.rcParams['font.sans-serif']=['SimSun']
plt.savefig("柱状图1.svg")
plt.show()
#绘制箱线图
# 箱线图上色
import matplotlib.pyplot as plt
list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
c_list = ['#ef476f', '#ffd166', '#118AD5'] # 颜色代码列表
# 绘制箱线图
plt.figure(figsize=(2,4))
f = plt.boxplot(list1, vert=True, sym='+b', showmeans=True,
meanline=True, patch_artist=True, widths=0.2)
for box, c in zip(f['boxes'], c_list): # 对箱线图设置颜色
box.set(color=c, linewidth=2)
box.set(facecolor=c)
plt.show()
#堆积百分比图
import matplotlib.pyplot as plt
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
first = [20, 34, 30, 35, 27]
second = [25, 32, 34, 20, 25]
third = [21, 31, 37, 21, 28]
fourth = [26, 31, 35, 27, 21]
data = [first, second, third, fourth]
x = range(len(labels))
width = 0.35
# 将bottom_y元素都初始化为0
bottom_y = [0] * len(labels)
# 计算每组柱子的总和,为计算百分比做准备
sums = [sum(i) for i in zip(first, second, third, fourth)]
plt.figure(figsize=(6,9))
for i in data:
# 计算每个柱子的高度,即百分比
y = [a/b for a, b in zip(i, sums)]
plt.bar(x, y, width, bottom=bottom_y)
# 计算bottom参数的位置
bottom_y = [(a+b) for a, b in zip(y, bottom_y)]
plt.xticks(x, labels)
plt.title('Percent stacked bar ')
plt.show()
标签:plt,show,python,labels,matplotlib,图表,np,import From: https://www.cnblogs.com/kun-sir/p/16743490.html