(一)再说legend()
import matplotlib.pyplot as pltimport numpy as npx = np.arange(0, 2.1, 0.1)y = np.power(x, 3)y1 = np.power(x, 2)y2 = np.power(x,1)plt.plot(x, y, ls="-", lw=2, label="$x^{3}$")plt.plot(x, y1, ls="-", lw=2, c="r", label="$x^{2}$")plt.plot(x, y2, ls="-", lw=2, c="y", label="$x^{1}$")plt.legend(loc="upper left", bbox_to_anchor=(0.05, 0.95), ncol=3, title="power function", shadow=True, fancybox=True)'''loc------->位置参数bbox_to_anchor------->线框位置参数,四元元祖 fancybox------>线框圆角'''plt.show()
(二)再说title()
import matplotlib.pyplot as pltimport numpy as npx = np.linspace(-2, 2, 1000)y = np.exp(x)plt.plot(x, y, ls="-", lw=2, color="g")plt.title("center demo")plt.title("Left Demo", loc="left", fontdict={ "size":"xx-large", "color":"r", "family":"Times New Roman"})#title的参数主要集中在位置和字体风格的设置,字体风格可以用字典也可以像下面一样用属性plt.title("right demo", loc = "right", family="Comic Sans MS",size=20, style="oblique", color="c")plt.show()
(三)xlim的逆序实现
import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as npmpl.rcParams["font.sans-serif"] = ["LiSu"]mpl.rcParams["axes.unicode_minus"] = Falsetime = np.arange(1, 11, 0.5)machinePower = np.power(time, 2)+.7plt.plot(time, machinePower, ls="-", lw=2, c="r")plt.xlim(10, 1)#实现xlim的逆序plt.xlabel("使用年限")plt.ylabel("机器功率")plt.title("机器损耗曲线")plt.grid(ls=":", lw=1, color="gray", alpha=.6)plt.show()
(四)带表格的饼状图
import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as npmpl.rcParams["font.sans-serif"] = ["SimHei"]mpl.rcParams["axes.unicode_minus"] = Falselabels = ["A难度水平", "B难度水平", "C难度水平", "D难度水平"]students = [0.35, 0.15, 0.20, 0.30]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)'''explode----->饼边缘偏离半径的百分比,出现分离的原因shadow------>阴影'''plt.title("选择不同难度测试试卷的学生占比")colLabels = ["A难度水平", "B难度水平", "C难度水平", "D难度水平"]rowLabels = ["学生选择试卷人数"]studentValues = [[350, 150, 200, 300]]colColors = ["#377eb8", "#4daf4a", "#984ea3", "#ff7f00"]plt.table(cellText=studentValues, cellLoc="center", colWidths=[0.1]*4, colLabels=colLabels, colColours=colColors, rowLabels=rowLabels, rowLoc="center", loc="upper left")'''cellText-------->表格中的数值,按照行排列cellLoc--------->表格中的数据对齐位置colWidth-------->每列的宽度colLable-------->每列的列名称rowColours------>每列的颜色rowLabels------->每行的名称rowLoc---------->行名称对齐方式loc------------->表格在画布中的位置'''plt.show()