Python数据可视化之matplotlib实践 源码 第一篇 入门 第三章
Posted Hello_BeautifulWorld
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python数据可视化之matplotlib实践 源码 第一篇 入门 第三章相关的知识,希望对你有一定的参考价值。
图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.14
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 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()
================================================
图 3.15
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 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()
================================================
图 3.16
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 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()
================================================
图 3.17
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 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()
================================================
图 3.18
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 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()
================================================
&nbs
以上是关于Python数据可视化之matplotlib实践 源码 第一篇 入门 第三章的主要内容,如果未能解决你的问题,请参考以下文章
Python数据可视化之matplotlib实践 源码 第一篇 入门 第三章
Python数据可视化之matplotlib实践 源码 第一篇 入门 第三章