第八次训练(一):Matplotlib初步¶
在了解了Numpy和Pandas之后,我们基本具有了一些数据处理和计算的能力,下一步是了解一些数据可视化的方法
在进行数据可视化时,我们最常使用的库是Matplotlib,通过Matplotlib我们可以将Numpy或者Pandas中的数据通过画图的方式更加直观的展示出来,方便我们分析。
在使用之前同样确保已经安装了Matplotlib库,使用conda或者pip来安装。
conda install matplotlib
# or
pip install matplotlib
In [3]:
Copied!
import numpy as np # 生成数据使用
import matplotlib.pyplot as plt # 导入包
import numpy as np # 生成数据使用
import matplotlib.pyplot as plt # 导入包
Matplotlib具有丰富的功能,能够根据我们的需求画出各种可视化的效果,并且提供了丰富的自定义选项和可配置项,以下展示一些简单直观的应用方法
1.1 常用图表类型¶
1.1.1 绘制折线图¶
In [5]:
Copied!
# 折线图
plt.plot([1,2,3,4,5],[1,2,3,4,5]) # 画出一条经过(1,1),(2,2,),(3,3,)....(5,5)的直线
plt.xlabel('xlabel',fontsize = 16) # 在x轴上标注内容
plt.ylabel('ylabel',fontsize = 16) # 在y轴上标注内容
plt.grid(True) # 显示网格
# 折线图
plt.plot([1,2,3,4,5],[1,2,3,4,5]) # 画出一条经过(1,1),(2,2,),(3,3,)....(5,5)的直线
plt.xlabel('xlabel',fontsize = 16) # 在x轴上标注内容
plt.ylabel('ylabel',fontsize = 16) # 在y轴上标注内容
plt.grid(True) # 显示网格
1.1.2 绘制散点图¶
In [7]:
Copied!
# 散点图
plt.scatter([1,2,3,4,5],[1,2,3,4,5])
plt.xlabel('xlabel') # 在x轴上标注内容
plt.ylabel('ylabel') # 在y轴上标注内容
# 散点图
plt.scatter([1,2,3,4,5],[1,2,3,4,5])
plt.xlabel('xlabel') # 在x轴上标注内容
plt.ylabel('ylabel') # 在y轴上标注内容
Out[7]:
Text(0, 0.5, 'ylabel')
1.1.3 绘制柱状图¶
In [9]:
Copied!
# 柱状图
x = np.linspace(1,11,10)
y = np.random.rand(10)
plt.bar(x,y)
# 柱状图
x = np.linspace(1,11,10)
y = np.random.rand(10)
plt.bar(x,y)
Out[9]:
<BarContainer object of 10 artists>
In [11]:
Copied!
# 横置柱状图
x = np.linspace(1, 11, 10)
y = np.random.rand(10)
plt.barh(x,y)
# 横置柱状图
x = np.linspace(1, 11, 10)
y = np.random.rand(10)
plt.barh(x,y)
Out[11]:
<BarContainer object of 10 artists>
1.1.4 绘制直方图¶
In [13]:
Copied!
# 直方图
x = np.random.randn(1000) # 生成1000个服从正态分布的数据
plt.hist(x,bins=30) # 画直方图,bin参数指定分成的组数
plt.show()
# 直方图
x = np.random.randn(1000) # 生成1000个服从正态分布的数据
plt.hist(x,bins=30) # 画直方图,bin参数指定分成的组数
plt.show()
1.1.5 绘制饼图¶
In [15]:
Copied!
# 饼图
plt.pie([1,1,1],labels=["a","b","c"])
plt.show()
# 饼图
plt.pie([1,1,1],labels=["a","b","c"])
plt.show()
1.1.6 绘制箱形图¶
Matplotlib可以方便地绘制箱型来观察数据的分布信息
箱形图提供了一种只用5个点对数据集做简单总结的方式。这5个点包括中点、Q1、Q3、分部状态的高位(max)和低位(min)。箱形图很形象的分为中心、延伸以及分布状态的全部范围。
In [17]:
Copied!
# 箱型图
x = np.random.randint(100,size=20)
print(x)
plt.boxplot(x) # 绘制箱型图
plt.show()
# 箱型图
x = np.random.randint(100,size=20)
print(x)
plt.boxplot(x) # 绘制箱型图
plt.show()
[92 5 33 4 77 9 44 77 85 90 0 39 65 52 51 90 91 5 98 76]
1.2 绘图设置¶
1.2.1 自定义样式¶
Matplotlib可以更改所画出的线条和点的颜色以及样式,线条对应的参数参考下表
| 字符 | 类型 | 字符 | 类型 |
|---|---|---|---|
'-' |
实线 | '--' |
虚线 |
'-.' |
虚点线 | ':' |
点线 |
'.' |
点 | ',' |
像素点 |
'o' |
圆点 | 'v' |
下三角点 |
'^' |
上三角点 | '<' |
左三角点 |
'>' |
右三角点 | '1' |
下三叉点 |
'2' |
上三叉点 | '3' |
左三叉点 |
'4' |
右三叉点 | 's' |
正方点 |
'p' |
五角点 | '*' |
星形点 |
'h' |
六边形点1 | 'H' |
六边形点2 |
'+' |
加号点 | 'x' |
乘号点 |
'D' |
实心菱形点 | 'd' |
瘦菱形点 |
'_' |
横线点 |
In [19]:
Copied!
plt.plot([1,2,3,4,5],[1,2,3,4,5],"-.") # 画出一条经过(1,1),(2,2,),(3,3,)....(5,5)的虚点折线
plt.xlabel('xlabel',fontsize = 16) # 在x轴上标注内容
plt.ylabel('ylabel',fontsize = 16) # 在y轴上标注内容
plt.title("matplolib") # 在图上显示标题
plt.grid(True) # 显示网格
plt.plot([1,2,3,4,5],[1,2,3,4,5],"-.") # 画出一条经过(1,1),(2,2,),(3,3,)....(5,5)的虚点折线
plt.xlabel('xlabel',fontsize = 16) # 在x轴上标注内容
plt.ylabel('ylabel',fontsize = 16) # 在y轴上标注内容
plt.title("matplolib") # 在图上显示标题
plt.grid(True) # 显示网格
同样可以控制颜色,不同颜色对应的参数如下表所示:
| 字符 | 颜色 |
|---|---|
‘b’ |
蓝色,blue |
‘g’ |
绿色,green |
‘r’ |
红色,red |
‘c’ |
青色,cyan |
‘m’ |
品红,magenta |
‘y’ |
黄色,yellow |
‘k’ |
黑色,black |
‘w’ |
白色,white |
In [21]:
Copied!
this_x = np.linspace(-9,9)
this_y = np.cos(this_x)
plt.plot(this_x,this_y,":",color='c') # 同时更改线条的样式和颜色
this_x = np.linspace(-9,9)
this_y = np.cos(this_x)
plt.plot(this_x,this_y,":",color='c') # 同时更改线条的样式和颜色
Out[21]:
[<matplotlib.lines.Line2D at 0x220703caf00>]
线条的宽度和透明度也可以修改
In [23]:
Copied!
this_x = np.linspace(-9,9)
this_y = np.cos(this_x)
# linewidth 参数设置线条宽度,alpha 参数设置线条透明度
plt.plot(this_x, this_y, linewidth=3.0, alpha=0.4)
this_x = np.linspace(-9,9)
this_y = np.cos(this_x)
# linewidth 参数设置线条宽度,alpha 参数设置线条透明度
plt.plot(this_x, this_y, linewidth=3.0, alpha=0.4)
Out[23]:
[<matplotlib.lines.Line2D at 0x220704599a0>]
1.2.2 绘制子图¶
Matplotlib除了可以画单个图像,也可以将多个图像画在同一个图中
In [25]:
Copied!
plt.plot([1,2,3,4],label="a")
plt.plot([4,3,2,1],label="b")
plt.legend(loc='best') # 显示图例
plt.plot([1,2,3,4],label="a")
plt.plot([4,3,2,1],label="b")
plt.legend(loc='best') # 显示图例
Out[25]:
<matplotlib.legend.Legend at 0x22070421280>
Matplotlib中还有一个子图的概念,可以达到下面的效果
In [27]:
Copied!
plt.subplot(211) # 当前画图是在一个2行1列的图中,目前画子图中的第一个图
plt.plot([1,2,3,4])
plt.plot([4,3,2,1])
plt.subplot(212) # 当前是在2行1列的图中,目前画子图中的第二个图
x = np.arange(10)
plt.plot(x,x**3)
plt.subplot(211) # 当前画图是在一个2行1列的图中,目前画子图中的第一个图
plt.plot([1,2,3,4])
plt.plot([4,3,2,1])
plt.subplot(212) # 当前是在2行1列的图中,目前画子图中的第二个图
x = np.arange(10)
plt.plot(x,x**3)
Out[27]:
[<matplotlib.lines.Line2D at 0x220715a9130>]
1.2.3 坐标轴设置¶
Matplotlib 中还可以设置图像坐标轴的刻度单位
In [29]:
Copied!
plt.subplot(211)
this_x = np.linspace(-9,9)
this_y = np.cos(this_x)
plt.plot(this_x, this_y)
plt.subplot(212)
plt.xticks(np.arange(-9,9,1))
plt.yticks(np.arange(-1,1.1,0.4))
plt.plot(this_x,this_y)
plt.subplot(211)
this_x = np.linspace(-9,9)
this_y = np.cos(this_x)
plt.plot(this_x, this_y)
plt.subplot(212)
plt.xticks(np.arange(-9,9,1))
plt.yticks(np.arange(-1,1.1,0.4))
plt.plot(this_x,this_y)
Out[29]:
[<matplotlib.lines.Line2D at 0x220716ae330>]
也可以设置坐标轴的显示范围,来实现显示某一指定区间内的图像
In [31]:
Copied!
plt.subplot(211)
this_x = np.linspace(-9,9)
this_y = np.cos(this_x)
plt.plot(this_x, this_y)
plt.subplot(212)
plt.xlim((-5, 5))
plt.ylim((-1.5, 1.5))
plt.plot(this_x,this_y)
plt.subplot(211)
this_x = np.linspace(-9,9)
this_y = np.cos(this_x)
plt.plot(this_x, this_y)
plt.subplot(212)
plt.xlim((-5, 5))
plt.ylim((-1.5, 1.5))
plt.plot(this_x,this_y)
Out[31]:
[<matplotlib.lines.Line2D at 0x2207173e300>]
1.2.4 图像大小¶
Matplotlib 中也可以设置整个图像大小
In [33]:
Copied!
this_x = np.linspace(-9,9)
this_y = np.cos(this_x)
plt.figure(figsize=(3.5, 3.5)) # 设置图像大小为 3.5 英寸乘以 3.5 英寸
plt.plot(this_x, this_y)
this_x = np.linspace(-9,9)
this_y = np.cos(this_x)
plt.figure(figsize=(3.5, 3.5)) # 设置图像大小为 3.5 英寸乘以 3.5 英寸
plt.plot(this_x, this_y)
Out[33]:
[<matplotlib.lines.Line2D at 0x220718ec050>]
In [35]:
Copied!
plt.figure(figsize=(7.5, 7.5)) # 设置图像大小为 7.5 英寸乘以 7.5 英寸
plt.plot(this_x,this_y)
plt.figure(figsize=(7.5, 7.5)) # 设置图像大小为 7.5 英寸乘以 7.5 英寸
plt.plot(this_x,this_y)
Out[35]:
[<matplotlib.lines.Line2D at 0x2207195c050>]
In [ ]:
Copied!