图3.1
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=[1,2,3,4,5]
y=[6,10,4,5,1]
plt.grid(True, axis='y',ls=':',color='r',alpha=0.3)
plt.bar(x,y,align='center', color='b', tick_label=['A','B','C','D','E'],
alpha=0.6, edgecolor="black")
plt.xlabel('测试难度')
plt.ylabel('试卷份数')
plt.show()
View Code
================================================
图3.2
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=[1,2,3,4,5]
y=[6,10,4,5,1]
plt.grid(True, axis='x',ls=':',color='r',alpha=0.3)
plt.barh(x,y,align='center', color='c', tick_label=['A','B','C','D','E'],
alpha=0.6, edgecolor="black")
plt.ylabel('测试难度')
plt.xlabel('试卷份数')
plt.show()
View Code
================================================
图 3.3
import matplotlib as mplimport matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=[1,2,3,4,5]
y=[6,10,4,5,1]
y1=[2,6,3,8,5]
plt.bar(x,y,align='center',color='#66c2a5', tick_label=['A','B','C','D','E'],
label='班级A', edgecolor='black')
plt.bar(x,y1,align='center',color='#8da0cb', bottom=y,
label='班级B', edgecolor='black')
plt.xlabel("测试难度")
plt.ylabel("测试份数")
plt.legend()
plt.show()
View Code
================================================
图 3.4
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=[1,2,3,4,5]
y=[6,10,4,5,1]
y1=[2,6,3,8,5]
plt.barh(x,y,align='center',color='#66c2a5', tick_label=['A','B','C','D','E'],
label='班级A', edgecolor='black')
plt.barh(x,y1,align='center',color='#8da0cb', left=y,
label='班级B', edgecolor='black')
plt.ylabel("测试难度")
plt.xlabel("测试份数")
plt.legend()
plt.show()
View Code
================================================
图 3.5
import matplotlib as mplimport matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=np.array([1,2,3,4,5])
y=[6,10,4,5,1]
y1=[2,6,3,8,5]
bar_width=0.35
tick_label=['A','B','C','D','E']
plt.bar(x, y, bar_width, align='center',color='c', label='班级A', alpha=0.5)
plt.bar(x+bar_width,y1,bar_width, align='center',color='b', label='班级B', alpha=0.5)
plt.xticks(x+bar_width/2, tick_label)
plt.xlabel("测试难度")
plt.ylabel("试卷份数")
plt.legend()
plt.show()
View Code
================================================
图 3.6
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=np.array([1,2,3,4,5])
y=[6,10,4,5,1]
y1=[2,6,3,8,5]
bar_width=0.35
tick_label=['A','B','C','D','E']
plt.barh(x, y, bar_width, align='center',color='c', label='班级A', alpha=0.5)
plt.barh(x+bar_width,y1,bar_width, align='center',color='b', label='班级B', alpha=0.5)
plt.yticks(x+bar_width/2, tick_label)
plt.ylabel("测试难度")
plt.xlabel("试卷份数")
plt.legend()
plt.show()
View Code
================================================
图 3.7
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=[1,2,3,4,5]
y=[6,10,4,5,1]
plt.bar(x,y, align='center', color='c', tick_label=['A','B','C','D','E'],
hatch='///')
plt.xlabel("测试难度")
plt.ylabel("试卷份数")
plt.show()
View Code
================================================
图 3.8
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(1,6,1)
y=[0,4,3,5,6]
y1=[1,3,4,2,7]
y2=[1,1,1,1,1]
labels=['BluePlanet', 'BrownPlanet', 'GreenPlanet']
colors=['#8da0cb','#fc8d62','#66c2a5']
plt.stackplot(x, y, y1, y2, labels=labels, colors=colors)
plt.legend(loc='upper left')
plt.show()
View Code
================================================
图 3.9
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
plt.broken_barh([(30,100),(180,50),(260,70)], (20,8), facecolors='#1f78b4')
plt.broken_barh([(60,90),(190,20),(230,30),(280,60)], (10,8),
facecolors=['#7fc97f','#beaed4','#fdc086','#ffff99'])
plt.xticks(np.arange(0,361,60))
plt.yticks([15,25],['歌剧院A','歌剧院B'])
plt.xlim(0, 360)
plt.ylim(5, 35)
plt.xlabel("演出时间(分)")
plt.grid(ls='-', lw=1, color='gray')
plt.title("不同地区的歌剧院的演出时间比较")
plt.show()
View Code
================================================
图 3.10
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=np.linspace(1,10,10)
y=np.sin(x)
plt.step(x,y,color='#8dd3c7', where='pre', lw=2)
plt.xlim(0, 11)
plt.ylim(-1.2, 1.2)
plt.xticks(np.arange(1, 11, 1))
plt.show()
View Code
================================================
图 3.11
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=np.linspace(1,10,10)
y=np.sin(x)
plt.step(x,y,color='#8dd3c7', where='post', lw=2)
plt.xlim(0, 11)
plt.ylim(-1.2, 1.2)
plt.xticks(np.arange(1, 11, 1))
plt.show()
View Code
================================================
图 3.12
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
scoresT=np.random.randint(0,100,100)
x=scoresT
bins=range(0,101,10)
plt.hist(x, bins, color='#377eb8', histtype='bar',rwidth=1.0, edgecolor="black")
plt.xlabel("测试成绩")
plt.ylabel("学生人数")
plt.show()
View Code
================================================
图 3.14
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
scoresT1=np.random.randint(0,100,100)
scoresT2=np.random.randint(0,100,100)
x=[scoresT1,scoresT2]
colors=['#8dd3c7','#bebada']
labels=['班级A','班级B']
bins=range(0,101,10)
plt.hist(x,bins=bins, color=colors, histtype='bar', edgecolor="black",
rwidth=1.0, stacked=True, label=labels)
plt.xlabel("测试成绩(分)")
plt.ylabel("学生人数")
plt.title("不同班级的测试成绩直方图")
plt.legend(loc="upper left")
plt.show()
View Code
================================================
图 3.15
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
scoresT1=np.random.randint(0,100,100)
scoresT2=np.random.randint(0,100,100)
x=[scoresT1,scoresT2]
colors=['#8dd3c7','#bebada']
labels=['班级A','班级B']
bins=range(0,101,10)
plt.hist(x,bins=bins, color=colors, histtype='bar', edgecolor="black",
rwidth=0.8, stacked=False, label=labels)
plt.xlabel("测试成绩(分)")
plt.ylabel("学生人数")
plt.title("不同班级的测试成绩直方图")
plt.legend(loc="upper left")
plt.show()
View Code
================================================
图 3.16
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
scoresT1=np.random.randint(0,100,100)
scoresT2=np.random.randint(0,100,100)
x=[scoresT1,scoresT2]
colors=['#8dd3c7','#bebada']
labels=['班级A','班级B']
bins=range(0,101,10)
plt.hist(x, bins=bins, color=colors, histtype='stepfilled', edgecolor="black",
rwidth=1.0, stacked=True, label=labels)
plt.xlabel("测试成绩(分)")
plt.ylabel("学生人数")
plt.title("不同班级的测试成绩的直方图")
plt.legend()
plt.show()
View Code
================================================
图 3.17
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
labels=['A 难度水平','B 难度水平','C 难度水平','D 难度水平']
students=[0.35, 0.15, 0.2, 0.3]
colors=['#377eb8','#4daf4a','#984ea3','#ff7f00']
explode=[0.1, 0.1, 0.1, 0.1]
plt.pie(students, explode=explode, labels=labels, autopct="%3.1f%%",
startangle=45, shadow=True, colors=colors)
plt.title("选择不同难度测试试卷的学生占比")
plt.show()
View Code
================================================
图 3.18
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
labels=['A 难度水平','B 难度水平','C 难度水平','D 难度水平']
students=[0.35, 0.15, 0.2, 0.3]
colors=['#377eb8','#4daf4a','#984ea3','#ff7f00']
explode=[0.1, 0.1, 0.1, 0.1]
#百分比数值pctdistance=0.7, 标签值labeldistance=1.2 以半径长度比例值作为显示依据
plt.pie(students, labels=labels, pctdistance=0.7, labeldistance=1.2,
autopct="%3.1f%%", startangle=45, colors=colors)
plt.title("选择不同难度测试试卷的学生占比")
plt.show()
View Code
================================================
图 3.19
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
elements=['面粉','砂糖','奶油','草莓酱','坚果']
weight1=[40,15,20,10,15]
weight2=[30,25,15,20,10]
colormapList=['#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00']
outer_colors=colormapList
inner_colors=colormapList
wedges1,texts1,autotexts1=plt.pie(weight1,autopct='%3.1f%%',radius=1.0, labels=elements,
pctdistance=0.80,labeldistance=1.1, colors=outer_colors,textprops=dict(color='black'),
wedgeprops=dict(width=0.4, edgecolor='w'))
wedges2,texts2,autotexts2=plt.pie(weight2,autopct='%3.1f%%',radius=0.6,
pctdistance=0.65,colors=inner_colors,textprops=dict(color='black'),
wedgeprops=dict(width=0.4, edgecolor='w'))
plt.legend(wedges1,elements, fontsize=12, title='配料表', loc="upper right",
bbox_to_anchor=(1.31, 1.0))
#设置百分比数值大小、粗细
plt.setp(autotexts1,size=13,weight='bold')
plt.setp(autotexts2,size=13,weight='bold')
#设置标签字体
plt.setp(texts1, size=13)
# plt.setp(texts2,size=12)
plt.title("不同果酱面包配料比例表")
plt.show()
View Code
================================================
图 3.20
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
plt.grid(axis='y', ls=':', lw=1, color='gray', alpha=0.4)
testA=np.random.randn(5000)
testB=np.random.randn(5000)
testList=[testA, testB]
labels=['随机数生成器AlphaRM','随机数生成器BetaRM']
colors=['#1b9e77','#d95f02']
#四分位间距的倍数,确定箱须包含数据的范围
whis=1.6
#箱体宽度
width=0.35
#patch_artist 是否给箱体加颜色, sym离群点形式
bplot=plt.boxplot(testList, whis=whis, widths=width, sym='o', labels=labels,
patch_artist=True)
for patch, color in zip(bplot['boxes'], colors):
patch.set_facecolor(color)
plt.ylabel("随机数值")
plt.title("生成器抗干扰能力的稳定性比较")
plt.show()
View Code
================================================
图 3.21
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
plt.grid(axis='y', ls=':', lw=1, color='gray', alpha=0.4)
testA=np.random.randn(5000)
testB=np.random.randn(5000)
testList=[testA, testB]
labels=['随机数生成器AlphaRM','随机数生成器BetaRM']
colors=['#1b9e77','#d95f02']
#四分位间距的倍数,确定箱须包含数据的范围
whis=1.6
#箱体宽度
width=0.35
#patch_artist 是否给箱体加颜色, sym离群点形式
bplot=plt.boxplot(testList, whis=whis, widths=width, sym='o', labels=labels,
patch_artist=True, notch=True)
for patch, color in zip(bplot['boxes'], colors):
patch.set_facecolor(color)
plt.ylabel("随机数值")
plt.title("生成器抗干扰能力的稳定性比较")
plt.show()
View Code
================================================
图 3.23
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=np.random.randn(1000)
plt.boxplot(x,vert=False)
plt.xlabel("随机数值")
plt.yticks([1],[""], rotation=90)
plt.ylabel('随机数生成器AlphaRM')
plt.grid(axis='x',ls=':', lw=1,color='gray', alpha=0.4)
plt.title("随机数生成器抗干扰能力的稳定性")
plt.show()
View Code
================================================
图 3.24
import matplotlib as mplimport matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=np.random.randn(1000)
plt.boxplot(x, vert=False, showfliers=False)
plt.xlabel("随机数值")
plt.yticks([1],[""], rotation=90)
plt.ylabel('随机数生成器AlphaRM')
plt.grid(axis='x',ls=':', lw=1,color='gray', alpha=0.4)
plt.title("随机数生成器抗干扰能力的稳定性")
plt.show()
View Code
================================================
图 3.25
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=np.linspace(0.1, 0.6, 10)
y=np.exp(x)
error=0.05+0.15*x
lower_error=error
upper_error=0.3*x
error_limit=[lower_error, upper_error]
plt.errorbar(x, y, yerr=error_limit, fmt=":o",
ecolor='y', elinewidth=4, ms=5,
mfc='c', mec='r', capthick=1, capsize=4)
plt.xlim(0, 0.7)
plt.show()
View Code
================================================
图 3.26
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x=np.arange(5)
y=[100,68,79,91,82]
std_err=[7,2,6,10,5]
error_attri=dict(elinewidth=2, ecolor='black', capsize=3)
plt.bar(x, y, color='c',width=0.6, align='center', yerr=std_err,
error_kw=error_attri, tick_label=['园区1', '园区2', '园区3', '园区4', '园区5'])
plt.xlabel("芒果种植区")
plt.ylabel("收割量")
plt.title("不同芒果种植区的单次收割量")
plt.grid(True, axis='y', ls=":", color="gray", alpha=0.2)
plt.show()
View Code
================================================
图 3.27
import matplotlib.pyplot as plt
import numpy as np
# 设置matplotlib正常显示中文和负号
matplotlib.rcParams['font.sans-serif']=['SimHei'] # 用黑体显示中文
matplotlib.rcParams['axes.unicode_minus']=False # 正常显示负号
x=np.arange(5)
y=[1200, 2400, 1800, 2200, 1600]
std_err=[150,100,180,130,80]
bar_width=0.6
colors=['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00']
plt.barh(x, y, bar_width, color=colors, align='center', xerr=std_err,
tick_label=['家庭', '小说', '心理', '科技', '儿童'])
plt.xlabel("订购数量")
plt.ylabel("图书种类")
plt.title("大型图书展销会的不同图书种类的采购情况")
plt.grid(True, axis='x', ls=':', color='gray', alpha=0.2)
plt.xlim(0, 2600)
plt.show()
View Code
================================================
图 3.28
import matplotlib.pyplot as plt
import numpy as np
# 设置matplotlib正常显示中文和负号
matplotlib.rcParams['font.sans-serif']=['SimHei'] # 用黑体显示中文
matplotlib.rcParams['axes.unicode_minus']=False # 正常显示负号
x=np.arange(5)
y1=[100, 68, 79, 91, 82]
y2=[120, 75, 70, 78, 85]
std_err1=[7, 2, 6, 10, 5]
std_err2=[5, 1, 4, 8, 9]
error_attri=dict(elinewidth=2, ecolor='black', capsize=3)
bar_width=0.4
tick_label=['园区1', '园区2', '园区3', '园区4', '园区5']
plt.bar(x, y1, bar_width, color='#87CEEB', align='center', yerr=std_err1,
error_kw=error_attri, label='2010')
plt.bar(x+bar_width, y2, bar_width, color='#CD5C5C', align='center', yerr=std_err2,
error_kw=error_attri, label='2013')
plt.xticks(x+bar_width/2, tick_label)
plt.grid(True, axis='y', ls=':', color='gray', alpha=0.2)
plt.legend()
plt.xlabel("芒果种植区")
plt.ylabel("收割量")
plt.title("不同芒果种植区的单次收割量")
plt.grid(True, axis='y', ls=":", color="gray", alpha=0.2)
plt.show()
View Code
================================================
图 3.29
import matplotlib.pyplot as plt
import numpy as np
# 设置matplotlib正常显示中文和负号
matplotlib.rcParams['font.sans-serif']=['SimHei'] # 用黑体显示中文
matplotlib.rcParams['axes.unicode_minus']=False # 正常显示负号
x=np.arange(5)
y1=[1200, 2400, 1800, 2200, 1600]
y2=[1050, 2100, 1300, 1600, 1340]
std_err1=[150, 100, 180, 130, 80]
std_err2=[120, 110, 170, 150, 120]
error_attri=dict(elinewidth=2, ecolor='black', capsize=0)
bar_width=0.6
tick_label=['家庭', '小说', '心理', '科技', '儿童']
plt.bar(x, y1, bar_width, color='#6495ED', align='center', yerr=std_err1,
error_kw=error_attri, label='地区1')
plt.bar(x, y2, bar_width, bottom=y1, color='#FFA500', align='center', yerr=std_err2,
error_kw=error_attri, label='地区2')
plt.xlabel("图书种类")
plt.ylabel("订购数量")
plt.xticks(x, tick_label)
plt.title("大型图书展销会的不同图书种类的采购情况")
plt.grid(True, axis='y', ls=':', color='gray', alpha=0.2)
plt.legend()
plt.show()
View Code
================================================