数据可视化学习笔记【三】(Matplotlib包)

数据可视化学习笔记【三】(Matplotlib包)

激动人心的画画时刻到了。

Cpt 1. 曲线

这是基于 Matlabplot 的绘图方式。在画图之前,需要先打开创建一个图,使用这个函数: plt.figure()plot 的参数内写入 xy 一个自变量,一个因变量。画出图像就可以了。

import matplotlib.pyplot as plt
import numpy as np
plt.style.use("seaborn-whitegrid")
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))

画出来的图象为:
在这里插入图片描述

展示多条曲线:

表示颜色:

  1. color="red"
  2. rgbcmyk 格式: color = " g" 这里省略了rb,输出的是绿色
  3. 0-1的灰度值:color="0.85"
  4. 16进制 RGB 格式: color="#FFDD45"
  5. RGB 元素:color=(0.5, 0.2, 0.9)
  6. HTML 颜色名称:color="chartreuse"

绘制多条曲线的时候,不用写 hold on,直接叠加 plt.plot() 就可以。

如果要绘制不同曲线:写在颜色之前 color = "--r" 就是红色虚线。

  1. - 实线
  2. -- 虚线
  3. -. 点划线
  4. : 实心点线

设置坐标轴上下限:
使用 plt.xlimplt.ylim 或是 axis=[xmin, xmax, ymin, ymax] 函数,这与 Matlab 中是一样的。

添加标题、坐标轴名称

这里用到的函数与 matlab 完全一致:plt.titleplt.xlabel 等参数就可以操作。

import matplotlib.pyplot as plt
import numpy as np

plt.style.use("seaborn-whitegrid")
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.xlabel("This is x")
plt.ylabel("This is y")
plt.title("This is title")
ax.legend(["sin", "cos"], frameon = True, framealpha = 0.8)

在这里插入图片描述

Remark:可以只设置部分,不用全部设置也是可以的。

Cpt 2. 散点图

plt.scatter 函数

plt.style.use("seaborn-whitegrid")

fig = plt.figure()
ax = plt.axes()

x = np.linspace(0, 10, 100)
plt.scatter(x, np.sin(x))
plt.scatter(x, np.cos(x))
plt.xlabel("This is x")
plt.ylabel("This is y")
plt.title("This is title")
ax.legend(["sin", "cos"], frameon = True, framealpha = 0.8)

在这里插入图片描述

plt.plot() 可以实现散点图的绘制。当然,也可以设置不同的形状:

plt.style.use("seaborn-whitegrid")

fig = plt.figure()
ax = plt.axes()
rng = np.random.RandomState(0)
markers = ["o", ".", "x", "+", "^", "<", "s", "d"]
for i in markers:
    plt.plot(rng.rand(5), rng.rand(5), i, label = "marker = {}".format(i))
    plt.legend(numpoints = 1)
    plt.xlim(0, 1.8)

在这里插入图片描述

如果用 scatter() 函数,绘制更加灵活。里面的参数有 sizealpha 设置透明度。

Cpt 3. 等高线

绘制方法与 Matlab 相同, meshgrid 函数将从一维数组变为二维坐标数组。 contour 是离散的,contourf 是连续的。同样可以使用 imshow 来绘制,但是没有坐标轴。

fig = plt.figure()
ax = plt.axes()
def f(x,y):
    return x**2 + y**2 - x*y
x = np.linspace(-10, 10, 50)
y = np.linspace(-10, 10, 50)
x,y = np.meshgrid(x, y)
z = f(x,y)
plt.contour(x, y, z, 50, cmap = "RdGy")
# contourf 是连续的,contour 是离散的

在这里插入图片描述

Cpt 4. 直方图

import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(0, 0.8, 1000)
y = np.random.normal(-2, 1, 1000)
z = np.random.normal(3, 2, 1000)

kwargs = dict(histtype = "stepfilled", alpha = 0.3, bins = 40)

plt.hist(x, **kwargs)
plt.hist(y, **kwargs)
plt.hist(z, **kwargs)

在这里插入图片描述

Cpt 5. 多子图

plt.subplot 绘制多子图。包含参数:行数,列数,索引值(子图编号)。注:编号从1开始,不是从0开始

plt.figure()
for i in range(1,7):
    plt.subplot(2,3,i)
    plt.text(0.5,0.5,str((2,3,i)), fontsize = 18, ha = "center")

在这里插入图片描述

子图之间可以共享坐标轴:

fig, ax = plt.subplots(2, 3, sharex = "col", sharey = "row")

但是要注意,要关掉子图的坐标轴的显示。

自定义合理利用空间的子图

用到 GridSpec 函数:

plt.figure()
grid = plt.GridSpec(2,3,wspace = 0.4, hspace = 0.3)
plt.subplot(grid[0,0])
plt.subplot(grid[0,1:])
plt.subplot(grid[1,:2])
plt.subplot(grid[1,2])

在这里插入图片描述

发布了12 篇原创文章 · 获赞 6 · 访问量 605

猜你喜欢

转载自blog.csdn.net/weixin_44618103/article/details/104196068