Python Matplotlib

🍦 Matplotlib是Python的一个低级图形绘制库,由John D.Hunter创建,大部分用Python编写,用作可视化实用程序。

1 Matplotlib

  • Matplotlib
    • 命令窗口下使用pip install Matplotlib进行安装。
    • 通过import关键字将其导入应用程序:import matplotlib
    • 检查Matplotlib版本,版本字符串存储在__version__属性下。
    • 大多数实用程序位于Pyplot子模块下,通常使用别名plt导入:import matplotlib.pyplot as plt
1
2
3
4
5
6
7
8
9
10
import matplotlib
import numpy as np
import matplotlib.pyplot as plt # Pyplot子模块

print(matplotlib.__version__) # 检查Matplotlib版本

xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)
plt.show()

2 绘图操作

  • 绘图操作
    • plot()函数用于在图表中绘制点,默认情况下,是从一点到另一点绘制一条线。
    • 采用参数指定图中的点,参数一包含x轴上点的数组,参数二包含y轴上点的数组。
    • x轴是水平轴,y轴是垂直轴。
1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

xpoints = np.array([1, 8]) # 在图中从位置(1, 3)到位置(8, 10)画一条线
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show()

2-1 无线绘图

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

xpoints = np.array([1, 8]) # 在图中从位置(1, 3)到位置(8, 10)画一条线
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints, "o") # 仅绘制标记,可以使用快捷字符串符号参数“o”
plt.show()

2-2 多点绘图

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

xpoints = np.array([1, 2, 6, 8]) # 绘制任意多的点,确保两个轴上的点数相同
ypoints = np.array([3, 8, 1, 10]) # 在图中画一条线,从位置(1, 3)到(2, 8),(6, 1),最后到(8, 10)

plt.plot(xpoints, ypoints)
plt.show()

2-3 默认X轴点

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10, 5, 7])

plt.plot(ypoints) # x轴上的点默认值取决于y点的长度
plt.show()

3 使用标记

  • 使用标记
    • 使用关键字参数marker来强调带有指定标记的每个点。
    • 标记参考
      • '*'',''x''X''+''P''s''D''d''p''H'
      • 'h''v''^''<''>''1''2''3''4''|''_'
1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker="o") # 关键字参数marker来强调带有指定标记的每个点
plt.show()

3-1 fmt

  • fmt
    • 快捷字符串表示法参数,marker|line|color
    • 画线参考:'-'solid':'dotted'--'dashed'-.'dashdot
    • 关键字参数linestyle或ls更改绘制线的样式,若在fmt中省略线值,则不会绘制任何线。
    • 颜色参考:'r''g''b''c''m''y''k''w'
    • 关键字参数color或c设置线条的颜色,支持使用十六进制颜色值和140种支持的颜色名
    • 使用关键字参数linewidth或lw来更改设置线条的宽度,该值是一个浮点数,以点为单位。
1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, "o:r") # 颜色标记
plt.show()

(1) 点虚线

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linestyle="dotted") # 关键字参数linestyle或ls更改绘制线的样式
plt.show()

(2) 长虚线

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linestyle="dashed") # 关键字参数linestyle或ls更改绘制线的样式
plt.show()

(3) 线条颜色

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, color="g") # 关键字参数color或c设置线条的颜色
plt.show()

(4) 十六进制颜色值

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, c="#4CAF50") # 十六进制颜色值
plt.show()

(5) 140种支持的颜色

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, c="hotpink") # 140种支持的颜色
plt.show()

(6) 更改设置线条行宽

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linewidth="20.5") # 关键字参数linewidth或lw更改设置线条的宽度
plt.show()

3-2 标记大小

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker="o", ms=20) # 关键字参数markersize或ms来设置标记的大小
plt.show()

3-3 标记颜色

  • 标记颜色
    • 使用关键字参数markeredgecolor或mec来设置标记的边缘外颜色。
    • 使用关键字参数markerfacecolor或mfc来设置标记的边缘内颜色。
    • 参见:140种支持的颜色名称

(1) 边缘外颜色

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker="o", ms=20, mec="r") # 关键字参数markeredgecolor或mec来设置标记的颜色
plt.show()

(2) 边缘内颜色

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker="o", ms=20, mfc="r") # 关键字参数markerfacecolor或mfc来设置标记的颜色
plt.show()

(3) mec和mfc着色

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker="o", ms=20, mec="r", mfc="r")
plt.show()

(4) 十六进制颜色值

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker="o", ms=20, mec="#4CAF50", mfc="#4CAF50")
plt.show()

(5) 140种支持的颜色

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker="o", ms=20, mec="hotpink", mfc="hotpink")
plt.show()

3-4 多线绘制

  • 多线绘制
    • 通过plt.plot()函数简单地绘制任意多条线。
    • 通过同一函数中为每条线添加x和y轴的点绘制。

(1) 指定绘制

1
2
3
4
5
6
7
8
9
10
import numpy as np
import matplotlib.pyplot as plt

y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])

plt.plot(y1) # 为每条线指定一个函数进行绘制
plt.plot(y2)

plt.show()

(2) x和y轴点

1
2
3
4
5
6
7
8
9
10
import numpy as np
import matplotlib.pyplot as plt

x1 = np.array([0, 1, 2, 3]) # 指定两条线的x和y点值进行绘制
y1 = np.array([3, 8, 1, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])

plt.plot(x1, y1, x2, y2)
plt.show()

4 添加标签

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.plot(x, y)

plt.xlabel("Average Pulse") # 向x轴和y轴添加标签
plt.ylabel("Calorie Burnage")

plt.show()

4-1 标题

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.plot(x, y)

plt.title("Sports Watch Data") # 为x轴和y轴添加标题
plt.xlabel("Average Pulse") # 向x轴和y轴添加标签
plt.ylabel("Calorie Burnage")

plt.show()

4-2 字体属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

font1 = {"family": "serif", "color": "blue", "size": 20}
font2 = {"family": "serif", "color": "darkred", "size": 15}

plt.title("Sports Watch Data", fontdict=font1) # 设置标题和标签的字体属性
plt.xlabel("Average Pulse", fontdict=font2)
plt.ylabel("Calorie Burnage", fontdict=font2)

plt.plot(x, y)
plt.show()

4-3 定位标题

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data", loc="left") # 合法值为left、right和center,默认值为center
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)
plt.show()

5 添加网格

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)
plt.grid() # 添加网格线
plt.show()

5-1 x轴网格

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)
plt.grid(axis="x") # x轴网格线
plt.show()

5-2 y轴网格

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)
plt.grid(axis="y") # y轴网格线
plt.show()

5-3 网格线属性

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)
plt.grid(color="green", linestyle="--", linewidth=0.5)
plt.show()

6 绘制子图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x, y) # 绘制2个图,plot 1,布局按行和列组织

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x, y) # 绘制2个图,plot 2,第三个参数指当前图的索引

plt.show()

6-1 彼此绘制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 1, 1)
plt.plot(x, y) # plot 1

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 1, 2)
plt.plot(x, y) # plot 2

plt.show()

6-2 绘制6个图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 1)
plt.plot(x, y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 2)
plt.plot(x, y)

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 3)
plt.plot(x, y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 4)
plt.plot(x, y)

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 5)
plt.plot(x, y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 6)
plt.plot(x, y)

plt.show()

6-3 每图添加标题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x, y) # plot 1
plt.title("SALES")

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x, y) # plot 2
plt.title("INCOME")

plt.show()

6-4 添加超极标题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x, y) # plot 1
plt.title("SALES")

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x, y) # plot 2
plt.title("INCOME")

plt.suptitle("MY SHOP") # 超级标题
plt.show()

7 绘制图形

  • 绘制图形
    • 散点图:scatter()函数,需要两个相同长度的数组,颜色图参考Available ColorMaps
    • 条形图:bar()函数,使用widthheight设置垂直条和水平条的宽度,默认值为0.8。
    • 直方图:hist()函数,使用一个数字数组来创建直方图,该数组作为参数发送到函数中。
    • 饼状图:pie()函数,第一个楔形的绘制从x轴开始(即起始角为0°开始),并逆时针移动。
      • 楔形颜色可以使用十六进制颜色值、也可以使用140种支持的颜色名称。
      • 'r''g''b''c''m''y''k''w'等快捷方式。

7-1 散点图

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])

plt.scatter(x, y)
plt.show()

(1) 比较图

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import matplotlib.pyplot as plt

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
plt.scatter(x, y)

x = np.array([2, 2, 8, 1, 15, 8, 12, 9, 7, 3, 11, 4, 7, 14, 12])
y = np.array([100, 105, 84, 105, 90, 99, 90, 95, 94, 100, 79, 112, 91, 80, 85])
plt.scatter(x, y)

plt.show()

(2) 设置颜色

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import matplotlib.pyplot as plt

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
plt.scatter(x, y, color="hotpink")

x = np.array([2, 2, 8, 1, 15, 8, 12, 9, 7, 3, 11, 4, 7, 14, 12])
y = np.array([100, 105, 84, 105, 90, 99, 90, 95, 94, 100, 79, 112, 91, 80, 85])
plt.scatter(x, y, color="#88c999")

plt.show()

(3) 给点上色

1
2
3
4
5
6
7
8
9
10
11
import numpy as np
import matplotlib.pyplot as plt

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
colors = np.array(
["red", "green", "blue", "yellow", "pink", "black", "orange",
"purple", "beige", "brown", "gray", "cyan", "magenta"])

plt.scatter(x, y, c=colors)
plt.show()

(4) 使用颜色图

1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap="viridis")
plt.show()

(5) 包含颜色图例

1
2
3
4
5
6
7
8
9
10
import numpy as np
import matplotlib.pyplot as plt

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap="viridis")
plt.colorbar()
plt.show()

(6) 更改点的大小

1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
sizes = np.array([20, 50, 100, 200, 500, 1000, 60, 90, 10, 300, 600, 800, 75])

plt.scatter(x, y, s=sizes)
plt.show()

(7) 调整点的透明度

1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
sizes = np.array([20, 50, 100, 200, 500, 1000, 60, 90, 10, 300, 600, 800, 75])

plt.scatter(x, y, s=sizes, alpha=0.5)
plt.show()

(8) 颜色结合透明度

1
2
3
4
5
6
7
8
9
10
11
import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(100, size=100)
y = np.random.randint(100, size=100)
colors = np.random.randint(100, size=100)
sizes = 10 * np.random.randint(100, size=100)

plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap="nipy_spectral")
plt.colorbar()
plt.show()

7-2 条形图

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x, y)
plt.show()

(1) 水平显示

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.barh(x, y)
plt.show()

(2) 条形颜色

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x, y, color="red")
plt.show()

(3) 十六进制颜色值

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x, y, color="#4CAF50")
plt.show()

(4) 140种支持的颜色

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x, y, color="hotpink")
plt.show()

(5) 设置垂直条的宽度

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x, y, width=0.1)
plt.show()

(6) 设置水平条的高度

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.barh(x, y, height=0.1)
plt.show()

7-3 直方图

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

x = np.random.normal(170, 10, 250)

plt.hist(x)
plt.show()

7-4 饼状图

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

y = np.array([35, 25, 25, 15])

plt.pie(y)
plt.show()

(1) 标签

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

y = np.array([35, 25, 25, 15])
labels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels=labels)
plt.show()

(2) 起始角

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt

y = np.array([35, 25, 25, 15])
labels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels=labels, startangle=90)
plt.show()

(3) 楔形脱离

1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt

y = np.array([35, 25, 25, 15])
labels = ["Apples", "Bananas", "Cherries", "Dates"]
explode = [0.2, 0, 0, 0]

plt.pie(y, labels=labels, explode=explode)
plt.show()

(4) 饼图阴影

1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt

y = np.array([35, 25, 25, 15])
labels = ["Apples", "Bananas", "Cherries", "Dates"]
explode = [0.2, 0, 0, 0]

plt.pie(y, labels=labels, explode=explode, shadow=True)
plt.show()

(5) 楔形颜色

1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt

y = np.array([35, 25, 25, 15])
labels = ["Apples", "Bananas", "Cherries", "Dates"]
colors = ["black", "hotpink", "b", "#4CAF50"]

plt.pie(y, labels=labels, colors=colors)
plt.show()

(6) 添加图例

1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt

y = np.array([35, 25, 25, 15])
labels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels=labels)
plt.legend()
plt.show()

(7) 图例标题

1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt

y = np.array([35, 25, 25, 15])
labels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels=labels)
plt.legend(title="Four Fruits:")
plt.show()

Python Matplotlib
https://stitch-top.github.io/2021/08/02/python/python09-python-matplotlib/
作者
Dr.626
发布于
2021年8月2日 23:45:26
许可协议