第十三章 Matplotlib库
数据可视化是数据分析的一个重要工具,掌声有请Matplotlib
13.0 环境配置
【1】 要不要plt.show()
-
ipython中可用魔术方法 %matplotlib inline
-
pycharm 中必须使用plt.show()
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use("seaborn-whitegrid") # 设置下面都是用这种风格
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y)
plt.ylabel("squares")
# plt.show()
Text(0, 0.5, 'squares')
【2】设置样式
plt.style.available[:5] # 查看可以使用的风格
['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight']
with plt.style.context("seaborn-white"): # with代码块中使用这个风格
plt.plot(x, y)
【3】将图像保存为文件
import numpy as np
x = np.linspace(0, 10 ,100)
plt.plot(x, np.exp(x))
plt.savefig("my_figure.png")
13.1 Matplotlib库
13.1.1 折线图
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use("seaborn-whitegrid")
import numpy as np
x = np.linspace(0, 2*np.pi, 100) # 0-2Π中间100个数
plt.plot(x, np.sin(x))
[<matplotlib.lines.Line2D at 0x18846169780>]
- 绘制多条曲线
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.cos(x))
plt.plot(x, np.sin(x))
[<matplotlib.lines.Line2D at 0x1884615f9e8>]
【1】调整线条颜色和风格
- 调整线条颜色
offsets = np.linspace(0, np.pi, 5)
colors = ["blue", "g", "r", "yellow", "pink"]
for offset, color in zip(offsets, colors):
plt.plot(x, np.sin(x-offset), color=color) # color可缩写为c
- 调整线条风格
x = np.linspace(0, 10, 11)
offsets = list(range(8))
linestyles = ["solid", "dashed", "dashdot", "dotted", "-", "--", "-.", ":"]
for offset, linestyle in zip(offsets, linestyles):
plt.plot(x, x+offset, linestyle=linestyle) # linestyle可简写为ls
- 调整线宽
x = np.linspace(0, 10, 11)
offsets = list(range(0, 12, 3))
linewidths = (i*2 for i in range(1,5))
for offset, linewidth in zip(offsets, linewidths):
plt.plot(x, x+offset, linewidth=linewidth) # linewidth可简写为lw
- 调整数据点标记
x = np.linspace(0, 10, 11)
offsets = list(range(0, 12, 3))
markers = ["*", "+", "o", "s"]
for offset, marker in zip(offsets, markers):
plt.plot(x, x+offset, marker=marker)
x = np.linspace(0, 10, 11)
offsets = list(range(0, 12, 3))
markers = ["*", "+", "o", "s"]
for offset, marker in zip(offsets, markers):
plt.plot(x, x+offset, marker=marker, markersize=10) # markersize可简写为ms
- 颜色跟风格设置的简写
x = np.linspace(0, 10, 11)
offsets = list(range(0, 8, 2))
color_linestyles = ["g-", "b--", "k-.", "r:"]
for offset, color_linestyle in zip(offsets, color_linestyles):
plt.plot(x, x+offset, color_linestyle)
x = np.linspace(0, 10, 11)
offsets = list(range(0, 8, 2))
color_marker_linestyles = ["g*-", "b+--", "ko-.", "rs:"]
for offset, color_marker_linestyle in zip(offsets, color_marker_linestyles):
plt.plot(x, x+offset, color_marker_linestyle)
其他用法及颜色缩写、数据点标记缩写等请查看官方文档,如下:
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot
【2】调整坐标轴
- xlim, ylim
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.xlim(-1, 7) # 坐标轴最大最小值范围
plt.ylim(-1.5, 1.5)
(-1.5, 1.5)
- axis
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.axis([-2, 8, -2, 2]) # 同时设置x轴和y轴,前两个是x轴,后两个是y轴
[-2, 8, -2, 2]
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.axis("tight") # 还可以设置图像的风格---紧致
(0.0, 6.283185307179586, -0.9998741276738751, 0.9998741276738751)
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.axis("equal") # 扁平
(0.0, 7.0, -1.0, 1.0)
?plt.axis
- 对数坐标
x = np.logspace(0, 5, 100)
plt.plot(x, np.log(x))
plt.xscale("log")
- 调整坐标轴刻度
x = np.linspace(0, 10, 100)
plt.plot(x, x**2)
plt.xticks(np.arange(0, 12, step=1)) # 直接设置坐标轴的刻度
x = np.linspace(0, 10, 100)
plt.plot(x, x**2)
plt.xticks(np.arange(0, 12, step=1), fontsize=15)
plt.yticks(np.arange(0, 110, step=10))
- 调整刻度样式
x = np.linspace(0, 10, 100)
plt.plot(x, x**2)
plt.tick_params(axis="both", labelsize=15)
【3