Python--Matplotlib (基本⽤法)
Matplotlib
Matplotlib 是Python中类似 MATLAB 的绘图⼯具,熟悉 MATLAB 也可以很快的上⼿ Matplotlib。
1. 认识Matploblib
1.1 Figure
在任何绘图之前,我们需要⼀个Figure对象,可以理解成我们需要⼀张画板才能开始绘图。
1.2 Axes
在拥有Figure对象之后,在作画前我们还需要轴,没有轴的话就没有绘图基准,所以需要添加Axes。也可以理解成为真正可以作画的纸。
上的代码,在⼀幅图上添加了⼀个Axes,然后设置了这个Axes的X轴以及Y轴的取值范围(这些设置并不是强制的,后⾯会再谈到关于这
些设置),效果如下图:
对于上⾯的fig.add_subplot(111)就是添加Axes的,参数的解释的在画板的第1⾏第1列的第⼀个位置⽣成⼀个Axes对象来准备作画。也可以通过fig.add_subplot(2, 2, 1)的⽅式⽣成Axes,前⾯两个参数确定了⾯板的划分,例如 2, 2会将整个⾯板划分成 2 * 2 的⽅格,第三个参数取值范围是 [1, 2*2] 表⽰第⼏个Axes。如下⾯的例⼦:
import matplotlib.pyplot as plt
fig = plt.figure()
fig = plt.figure ()
ax = fig.add_subplot (111)
ax.set (xlim =[0.5, 4.5], ylim =[-2, 8], title ='An Example Axes',
ylabel ='Y-Axis', xlabel ='X-Axis')
plt.show ()
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(224)
1.3 Multiple Axes
可以发现我们上⾯添加 Axes 似乎有点弱鸡,所以提供了下⾯的⽅式⼀次性⽣成所有 Axes:
fig, axes = plt.subplots(nrows=2,ncols=2)
axes[0,0].set(title='Upper Left')
axes[0,1].set(title='Upper Right')
axes[1,0].set(title='Lower Left')
axes[1,1].set(title='Lower Right')
fig 还是我们熟悉的画板, axes 成了我们常⽤⼆维数组的形式访问,这在循环绘图时,额外好⽤。
1.4 Axes Vs .pyplot
相信不少⼈看过下⾯的代码,很简单并易懂,但是下⾯的作画⽅式只适合简单的绘图,快速的将图绘出。在处理复杂的绘图⼯作时,我们还是需要使⽤ Axes 来完成作画的。
plt.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue',linewidth=3)
plt.xlim(0.5, 4.5)
matplotlib中subplotplt.show()
2. 基本绘图2D
2.1 线
plot()函数画出⼀系列的点,并且⽤线将它们连接起来。看下例⼦:
x = np.linspace(0, np.pi)
y_sin = np.sin(x)
y_cos = np.cos(x)
ax1.plot(x, y_sin)
ax2.plot(x, y_sin, 'go--', linewidth=2,markersize=12)
ax3.plot(x, y_cos, color='red',marker='+',linestyle='dashed')
在上⾯的三个Axes上作画。plot,前⾯两个参数为x轴、y轴数据。ax2的第三个参数是 MATLAB风格的绘图,对应ax3上的颜⾊,marker,线型。
另外,我们可以通过关键字参数的⽅式绘图,如下例:
x = np.linspace(0, 10, 200)
data_obj = {'x': x,
'y1': 2 * x + 1,
'y2': 3 * x + 1.2,
'mean': 0.5 * x * np.cos(2*x) + 2.5 * x + 1.1}
fig, ax = plt.subplots()
#填充两条线之间的颜⾊
ax.fill_between('x', 'y1', 'y2', color='yellow', data=data_obj)
# Plot the "centerline" with `plot`
ax.plot('x', 'mean', color='black', data=data_obj)
plt.show()
发现上⾯的作图,在数据部分只传⼊了字符串,这些字符串对⼀个这 data_obj 中的关键字,当以这种⽅式作画时,将会在传⼊给 data 中
寻对应关键字的数据来绘图。
2.2 散点图
只画点,但是不⽤线连接起来。
2.3 条形图条形图分两种,⼀种是⽔平的,⼀种是垂直的,见下例⼦:
x = np.arange(10)
y = np.random.randn(10)
plt.scatter(x, y, color ='red', marker='+')
plt.show()
np.random.seed(1)
x = np.arange(5)
y = np.random.randn(5)
fig, axes = plt.subplots(ncols=2,figsize=plt.figaspect(1./2))
vert_bars = axes[0].bar(x, y, color='lightblue',align='center')
horiz_bars = axes[1].barh(x, y, color='lightblue',align='center')
#在⽔平或者垂直⽅向上画线
axes[0].axhline(0, color='gray',linewidth=2)
axes[1].axvline(0, color='gray',linewidth=2)
plt.show()
条形图还返回了⼀个Artists 数组,对应着每个条形,例如上图 Artists 数组的⼤⼩为5,我们可以通过这些 Artists 对条形图的样式进⾏更改,如下例:
fig, ax = plt.subplots()
vert_bars = ax.bar(x, y, color='lightblue', align='center')
# We could have also done this with two separate calls to `ax.bar` and numpy boolean indexing.
for bar, height in zip(vert_bars, y):
if height < 0:
bar.set(edgecolor='darkred', color='salmon', linewidth=3)
plt.show()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论