13.Matplotlib库
13.1. 基本配置
数据可视化是数据分析的一个重要工具,最常用的就是Matplotlib库
【1】 要不要plt.show()
ipython中可用魔术方法 %matplotlib inline,这样可以无需plt.show()
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()
【2】设置样式
plt.style.available[:5]
['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight']
with plt.style.context("seaborn-white"):
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.2.Matplotlib库
13.2.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)
plt.plot(x, np.sin(x))
绘制多条曲线
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.cos(x))
plt.plot(x, np.sin(x))
【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)
其他用法及颜色缩写、数据点标记缩写等请查看官方文档,如下:
【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)
axis
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.axis([-2, 8, -2, 2])
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.axis("tight") # tight表示紧凑的图像
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.axis("equal") # equal表示扁平的图像
?plt.axis # 查看还有哪些样式
'on' Turn on axis lines and labels.
...
'square' Square plot; similar to 'scaled', but initially forcing
对数坐标
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, 1