第一章:使用函数绘制matplotlib的图表组成元素
1.1 绘制matplotlib的主要函数
1.2 准备数据
numpy包生产数据,linspace(0.5,3.5,100)在0.5到3.5之间均匀取100个数;randn(100)表示在标准正态分布中取100个数。
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
x = np.linspace(0.5,3.5,100)
y = np.sin(x)
y1 = np.random.randn(100)
plt.plot(x,y)
plt.plot(x,y1)
1.3 绘制matplot图表组成元素的函数用法
下面用函数的形式学习绘图。
1.3.1 函数plot()——展现变量的趋势变化
plt.plot(x,y,ls="-",lw=2label="plot figure")
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.cos(x)
plt.plot(x,y,ls="-",lw=2,label="Plot figure")
plt.legend()
plt.show()
1.3.2 函数 scatter() ——寻找变量之间的关系
plt.scatter(x,y1,c="b",label="scatter figure")
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.random.randn(1000)
plt.scatter(x,y,c="r",label="Plot figure")
plt.legend()
plt.show()
1.3.3 函数xlim() —— 设置X轴的数值显示范围
plt.xlim(xmin,xmax)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.random.randn(1000)
plt.scatter(x,y,c="r",label="Plot figure")
plt.legend(loc="upper right")
plt.xlim(0.05,10)
plt.ylim(0,1)
plt.show()
1.3.4 xlabel()和ylabel()——设置x轴(y轴)的标签文本
plt.xlabel(string)
plt.ylabel(string)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="Plot figure")
plt.legend(loc="upper right")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()
1.3.5 grid()——绘制刻度线的网格
plt.grid(linestyle=":",color="r")
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="Plot figure")
plt.legend(loc="upper right")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.grid(linestyle=":",color="r")
plt.show()
1.3.6 axhline()和axvline()—— 绘制平行于x轴的水平线参考系(平行于y轴的竖直参考线)
plt.axhline(y=0.0,c='r',ls='--',lw=2)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="Plot figure")
plt.legend(loc="upper right")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.axhline(y=0,c='r',ls='-.',lw=2)
plt.axvline(x=4,c='y',ls='--',lw=2)
plt.show()
1.3.7 函数axvspan()和axhspan()——绘制垂直于x轴或平行于y轴的参考区域
plt.axvspan(xmin=1.0,xman=2.0,facecolor='r',alpha=0.3)
plt.axhspan(ymin=1.0,yman=2.0,facecolor='r',alpha=0.3)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="Plot figure")
plt.legend(loc="upper right")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.axvspan(xmin=4.0,xmax=6.0,facecolor='r',alpha=0.3)
plt.axhspan(ymin=0,ymax=0.5,facecolor='y',alpha=0.3)
plt.show()
1.3.8 函数annotate() —— 添加图形内容细节的指向型注释文本
plt.annotate(string,xy=(np.pi/2,1.0),xytext=((np.pi/2)+0.15,1.5),weight="bold",color="y",arrowprops=dict(arrowstyle="->",connectionstyle="arc3"color="b"))
s: 注释的内容,一段文字;
xytext: 这段文字所处的位置;
xy: 箭头指向的位置;
arrowprops: 通过arrowstyle表明箭头的风格或种类。
这个函数比较复杂,可以看看这篇介绍,传送-
箭头种类(arrowstyle)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="Plot figure")
plt.legend(loc="upper right")
plt.annotate(r"maximum",
xy=(np.pi/2,1.0),
xytext=((np.pi/2)+1.0,0.8),
weight="bold",
color="y",
arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b"))
plt.show()
1.3.9 函数text() —— 添加图形内容细节的无指向型注释文本
plt.text(x,y,string,weight="bold",color="b")
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="Plot figure")
plt.legend(loc="upper right")
plt.text(3.10,0.10,"y=sin(x)",weight="bold",color="b")
plt.show()
1.3.10 函数title() —— 添加图形内容的标题
plt.title(string)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="Plot figure")
plt.legend(loc="upper right")
plt.title("y=sin(x)")
plt.xlabel("x-label")
plt.ylabel("y-label")
plt.text(3.10,0.10,"y=sin(x)",weight="bold",color="b")
plt.show()
1.3.10 函数legend() —— 添加不同图形的文本标签图例
plt.legend(loc="upper right")
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05,10,1000)
y = np.sin(x)
plt.plot(x,y,ls="-.",lw=2,c="c",label="Plot figure")
plt.legend(loc="lower right")
plt.show()
1.4 函数组合应用
整理前面所学的函数:
- plot()
- scatter()
- xlim()
- xlabel()
- grid()
- axline()
- axvspan()
- text()
- title()
- legend()
import matplotlib.pyplot as plt
import numpy as np
# data
x = np.linspace(0.5,3.5,100)
y = np.sin(x)
y1 = np.random.randn(100)
# scatter figure
plt.scatter(x,y1,c="r",label="scatter figure")
# plot figure
plt.plot(x,y,ls="--",lw=2,label="plot figure")
for spine in plt.gca().spines.keys():
if spine == "top" or spine == "right":
plt.gca().spines[spine].set_color("none")
plt.gca().xaxis.set_ticks_position("bottom")
plt.gca().yaxis.set_ticks_position("left")
plt.grid(True,ls=":",color="r")
plt.axhline(y=0.0,c="r",ls="--",lw=2)
plt.axvspan(xmin=1.0,xmax=2.0,facecolor="y",alpha=0.3)
plt.annotate(r"maximum",
xy=(np.pi/2,1.0),
xytext=((np.pi/2)+0.15,1.5),
weight="bold",
color="r",
arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b"))
plt.annotate(r"spines",
xy=(0.75,-3),
xytext=(0.35,-2.25),
weight="bold",
color="r",
arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b"))
plt.annotate(r"",
xy=(0,-2.78),
xytext=(0.4,-2.32),
weight="bold",
color="r",
arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b"))
plt.annotate(r"",
xy=(3.5,-2.98),
xytext=(3.6,-2.70),
weight="bold",
color="r",
arrowprops=dict(arrowstyle="->",connectionstyle="arc3",color="b"))
plt.text(3.6,-2.70,"'|' is tickline",weight="bold",color="b")
plt.text(3.6,-2.95,"3.5 is ticklabel",weight="bold",color="b")
plt.xlim(0,4)
plt.ylim(-3.0,3.0)
plt.title("y=sin(x)")
plt.xlabel("x-label")
plt.ylabel("y-label")
# legend()
plt.legend(loc="upper right")
plt.show()
references
1、《Python数据可视化之matplotlib实践》 刘大成著