1.在绘图区域中可能会出现多个图形,而这些图形如果不加以说明,观察者则很难识别出这些图形的主要内容。因此我们需要给这些图形添加标签说明,用以标记每个图形所代表的内容。用函数legend()和函数title()来实现。
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(-2*np.pi,2*np.pi,200)
y = np.sin(x)
y1 = np.cos(x)
plt.plot(x,y,label=r"$\sin(x)$")#表达式文本会以斜体形输出,呈现印刷级别的效果
plt.plot(x,y1,label="cos(x)")
plt.legend(loc="lower left")
plt.title("正弦函数和余弦函数的折线图")
plt.show()
正弦余弦
2.图例的展示样式调整,通过函数legend()关键字参数实现
import matplotlib.pyplot as plt
import numpy as np
x = 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)
plt.show()
样式调整
3.标题展示样式的调整通过title()关键字参数得以实现,这些关键字参数包含字体样式、字体大小、字体风格和字体颜色等文本属性。
import matplotlib.pyplot as plt
import numpy as np
x = 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"})
plt.title("right demo",loc="right",
family="comic sans MS",
size=20,
style="oblique",
color="c")
plt.show()
标题
4.带图例的饼图
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False
elements=["面粉","砂糖","奶油","草莓酱","坚果"]
weight=[40,15,20,10,15]
colors=["#e41a1c","#377eb8","#4daf4a","#984ea3","#ff7f00"]
wedges,texts,autotexts=plt.pie(weight,
autopct="%3.1f%%",
colors=colors,
textprops=dict(color="w"))
plt.legend(wedges,
elements,
fontsize=12,
title="配料表",
loc="center left",
bbox_to_anchor=(0.91,0,0.3,1))
plt.setp(autotexts,size=15,weight="bold")
plt.setp(texts,size=12)
plt.title("不同果酱面包配料比例表")
plt.show()
饼图
5.调整刻度范围和刻度标签,通过函数xlim()和函数xticks()来实现
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2*np.pi,2*np.pi,200)
y = np.sin(x)
plt.subplot(211)
plt.plot(x,y)
plt.subplot(212)
plt.xlim(-2*np.pi,2*np.pi)
plt.xticks([-2*np.pi,-3*np.pi/2,-1*np.pi,-1*(np.pi)/2,0,(np.pi)/2,np.pi,3*np.pi/2,2*np.pi],
["$-2\pi$","$-3\pi/2$","$-\pi$","$-\pi/2$","$0$","$\pi/2$","$\pi/2$","$-3\pi/2$","$2\pi$"])
plt.plot(x,y)
plt.show()
刻度调整
6.逆序设置坐标轴刻度标签,通过调整函数xlim()的参数内容来实现逆序展示刻度标签的可视化需求,我们可以根据具体的展示需求来灵活调整坐标轴刻度标签的数值排序方向,轻松实现升序和降序的需求。
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
time = np.arange(1,11,0.5)
machinepower = np.power(time,2)+0.7
plt.plot(time,machinepower,
linestyle="-",
linewidth=2,
color="r")
plt.xlim(10,1)
plt.xlabel("使用年限")
plt.ylabel("机器功率")
plt.title("机器损耗曲线")
plt.grid(ls=":",lw=1,color="gray",alpha=0.5)
plt.show()
逆序
7.向统计图形添加表格,数据可视化的主要作用就是直观地解释数据,使观察者可以发现数据背后的规律或是趋势变化。但是有时候为了更加全面的凸显数据的规律和特点,需要将统计图形和数据表格结合使用。
import matplotlib as mpl
import matplotlib.pyplot as plt
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]
explode = [0.1,0.1,0.1,0.1]
colors = ["#e41a1c","#377eb8","#4daf4a","#984ea3"]
plt.pie(students,
explode=explode,
labels=labels,
autopct="%1.1f%%",
startangle=45,
shadow= True,
colors=colors)
plt.title("选择不同难度测试试卷的学生占比")
colLabels = ["A难度水平","B难度水平","C难度水平","D难度水平"]
rowLabels = ["学生选择试卷人数"]
studentValues = [[350,150,200,300]]
colColors = ["#e41a1c","#377eb8","#4daf4a","#984ea3"]
plt.table(cellText=studentValues,
cellLoc="center",
colWidths=[0.3]*4,
colLabels=colLabels ,
colColours=colColors,
rowLabels=rowLabels,
rowLoc="center",
loc="bottom")
plt.show()
图表结合
网友评论