图3.1
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]
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()
================================================
图3.2
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]
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()
================================================
图 3.3
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.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()
================================================
图 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()
================================================
图 3.5
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.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()
================================================
图 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()
================================================
图 3.7
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]
plt.bar(x,y, align='center', color='c', tick_label=['A','B','C','D','E'],
hatch='///')
plt.xlabel("测试难度")
plt.ylabel("试卷份数")
plt.show()
================================================
图 3.8
import matplotlib as mpl
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()
================================================
图 3.9
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.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()
================================================
图 3.10
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(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()
================================================
图 3.11
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(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()
================================================
图 3.12
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
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()
================================================
图 3.13
================================================
来源:oschina
链接:https://my.oschina.net/u/4317114/blog/4279390