PyPlot
大多数matplotlib实用程序位于pyplot子模块下,通常以plt别名导入:
在图中从位置(0,0)到位置(6,250)画一条线
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)
plt.show()

绘图
绘制x和y点
plot()函数用于在图表中绘制点(标记).
默认情况下,plot()函数从点到点绘制一条线.
该函数采用参数来指定图中的点
参数1是一个包含x轴上的点的数组
参数2是一个包含y轴上的点的数组
如果我们需要绘制一条从(1,3)到(8,10)的线,我们必须将两个数组[1,8]和[3,10]传递给plot函数
import matplotlib.pyplot as plt
import numpy as np
#在图中从位置(1,3)到位置(8,10)画一条线:
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints)
plt.show()

无线绘图
仅绘制标记,可以使用快捷字符串符号参数“o”,这意味着“环”.
#在图中绘制两个点,一个在位置(1,3),一个在位置(8,10):
plt.plot(xpoints, ypoints, 'o')
plt.show()

多点
可以根据需要绘制任意数量的点,只需要确保两个轴上的点数相同即可。
# 在图中画一条线,从位置(1,3)到(2,8)然后到(6,1)最后到位置(8,10):
xpoints = np.array([1, 2, 6, 8])
ypoints = np.array([3, 8, 1, 10])
plt.plot(xpoints, ypoints)
plt.show()

默认X点
如果我们不指定x轴上的点,他们将获得默认值0、1、2、3(等等,取决于y点的长度).
因此,如果我们采用与上述相同的示例,并省略x点,则图表将如下所示:
ypoints = np.array([3, 8, 1, 10, 5, 7])
plt.plot(ypoints)
plt.show()

标记
标记
可以使用关键字参数maker,用指定的标记强调每个点:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker='o')
plt.show()

# 用星星标记每个点
plt.plot(ypoints, marker='*')
[<matplotlib.lines.Line2D at 0x26378acabe0>]

标记/线/颜色参考
标记有很多别的符号,用的时候可以再查(标记、线的形状、颜色)
格式化字符串fmt
可以使用快捷字符串符号参数来指定标记。
此参数也称为fmt,使用一下语法编写:
marker|line|color
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, 'o:r') #点是o,中间线是虚线,颜色是红色
plt.show()

标记尺寸
可以使用关键字参数markersize或ms来设置标记的大小:
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker='o', ms=20)
plt.show()

标记颜色
#使用关键字参数 markeredgecolor 或 mec来设置标记边缘的颜色:
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker='o', ms=20, mec='r')
plt.show()

#关键字参数 markerfacecolor 或较短的mfc来设置标记边缘内的颜色
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker='o', ms=20, mfc='r')
plt.show()

ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker='o', ms=20, mec='r', mfc='r')
plt.show()

#还可以使用十六进制颜色值
plt.plot(ypoints, marker='o', ms=20, mec='#4CAF50', mfc='#4CAF50')
[<matplotlib.lines.Line2D at 0x26378e5f908>]

#140种支持的颜色名称中的任何一种
plt.plot(ypoints, marker='o', ms=20, mec='hotpink', mfc='hotpink')
[<matplotlib.lines.Line2D at 0x26378ec8470>]

线条
线型
可以使用关键字参数linestyle 或 ls来更改绘制线的样式:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, linestyle='dotted')
plt.show()

plt.plot(ypoints, linestyle='dashed')
[<matplotlib.lines.Line2D at 0x26378f62278>]

更短的语法
线条样式可以用更短的语法编写:
linestyle 可以写成ls
dotted可以写成:
dashed可以写成–
plt.plot(ypoints, ls=':')
[<matplotlib.lines.Line2D at 0x26378de89e8>]

线参考
线的形式有多样,具体用到再进行了解
线条颜色
使用关键字参数color或c来设置线条的颜色:
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, color='r')
plt.show()

# 可以使用十六进制颜色值:
plt.plot(ypoints, c='#4CAF50')
[<matplotlib.lines.Line2D at 0x26378d08160>]

# 140中支持的颜色名称中的任何一种
plt.plot(ypoints, c='hotpink')
[<matplotlib.lines.Line2D at 0x26378b853c8>]

线宽度
可以使用关键字参数linewidth或lw来更改线条的宽度.
该值是一个浮点数,以磅为单位:
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, linewidth='20.5')
plt.show()

多行
通过简单的添加更多plt.plot()函数来绘制任意数量的线:
y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])
plt.plot(y1)
plt.plot(y2)
plt.show()

还可以通过在同一plt.plot()函数中为每条线添加x轴和y轴的点来绘制多条线.
(在上面的例子中,我们只制定了y轴上的点,这意味着x轴上的点得到了默认值(0、1、2、3)。)
x和y值成对出现:
x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 0])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])
plt.plot(x1, y1, x2, y2)
plt.show()

x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 0])
x2 = np.array([5, 7, 8, 4])
y2 = np.array([6, 2, 7, 11])
plt.plot(x1, y1, x2, y2)
plt.show()

标签
为绘图创建标签
使用pyplot,可以使用xlabel()和ylabel()函数为x轴和y轴设置标签。
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['KaiTi'] #设置字体为楷体
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("平均脉搏")
plt.ylabel("卡路里消耗")
plt.show()

为绘图创建标题
使用pyplot,可以使用title()函数为绘图设置标题。
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("运动手表数据")
plt.xlabel("平均脉搏")
plt.ylabel("卡路里消耗")
plt.show()

设置标题和标签的字体属性
使用xlabel(),ylabel()和title()中的fontdict参数来设置标题和标签的字体属性。
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': 'KaiTi', 'color': 'blue', 'size': 20}
font2 = {'family': 'KaiTi', 'color': 'darkred', 'size': 15}
plt.title("运动手表数据", fontdict=font1)
plt.xlabel("平均脉搏", fontdict=font2)
plt.ylabel("卡路里消耗", fontdict=font2)
plt.plot(x, y)
plt.show()

定位标题
使用title()中的loc参数来定位标题。
合法值是:“left”、“right"和"center”。默认值为"center"。
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("运动手表数据", loc='left')
plt.xlabel("平均脉搏")
plt.ylabel("卡路里消耗")
plt.plot(x, y)
plt.show()

网格
向绘图中添加网格线
使用pyplot,使用grid()函数将网格线添加到绘图中。
import numpy as np
import matplotlib
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("运动手表数据")
plt.xlabel("平均脉搏")
plt.ylabel("卡路里消耗")
plt.plot(x, y)
plt.grid()
plt.show()

指定要显示的网格线
使用grid()函数中的轴参数来指定要显示的网格线
合法值为:‘x’、‘y’和‘both’。默认值为“both”。
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("运动手表数据", loc='left')
plt.xlabel("平均脉搏")
plt.ylabel("卡路里消耗")
plt.plot(x, y)
plt.grid(axis='x') # 只显示x轴的网格线
plt.show()

设置网格的线属性
可以设置网格的线条属性,如下所示:
grid(color=‘color’,linestyle=‘linestyle’,linewidth=number)
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("运动手表数据", loc='left')
plt.xlabel("平均脉搏")
plt.ylabel("卡路里消耗")
plt.plot(x, y)
plt.grid(color='green', linestyle='--', linewidth=1.5) # 只显示x轴的网格线
plt.show()

多图
显示多个图
使用subplots()函数,可以在一张图中绘制多个图:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1) #整张图有一行两列 也就是两个子图 最后一个一代表第一张子图
plt.plot(x, y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2) #第二张子图
plt.plot(x, y)
plt.show()

subplots()函数
subplots()函数采用三个参数来描述图形的布局。
布局按行和列组织, 由第一个和第二个参数表示。 第三个参数表示当前绘图的索引。
如果我们想要一个2行1列的图形(这意味着两个图像将垂直显示而不是并排显示),我们可以编写如下语法:
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 1, 1)
plt.plot(x, y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 1, 2) #第二张子图
plt.plot(x, y)
plt.show()

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 1)
plt.plot(x, y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 2) #第二张子图
plt.plot(x, y)
#plot 3:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 3)
plt.plot(x, y)
#plot 4:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 4)
plt.plot(x, y)
#plot 5:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 5)
plt.plot(x, y)
#plot 6:
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()

标题
使用title()函数为每个图添加标题:
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title("SALES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2) #第二张子图
plt.plot(x, y)
plt.title("INCOME")
plt.show()

超级标题
可以使用suptitle()函数为整个图形添加标题:
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title("SALES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2) #第二张子图
plt.plot(x, y)
plt.title("INCOME")
plt.suptitle("MY SHOP")
plt.show()

散点图
创建散点图
借助PYPLOT,可以使用scatter()函数绘制散点图。
scatter()函数为每个观察绘制一个点。它需要两个长度相同的数组,一个用于x轴,一个用于y轴上的值:
import matplotlib.pyplot as plt
import numpy as np
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()

颜色
使用color或c参数为每一个散点图设置自己的颜色:
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()

为每个点上色
可以通过使用颜色数组作为c参数的值来为每个点设置特定颜色,
不能为此使用color参数,只能使用c参数。
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()

颜色图
Matplotlib模块有许多可用的颜色图。
颜色图就像一个颜色列表,其中每种颜色都有一个范围从0到100的值。
下面是一个颜色图的例子:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0NlgxkS9-1659780917433)(attachment:image.png)]
此颜色图称为“viridis”,它的范围从0(紫色)到100(黄色)。
如何使用颜色图
可以使用带有颜色图值的关键字参数cmap指定颜色图,在本例中为“viridis”,它是Matplotlib中可用的内置颜色图之一。
此外,还必须创建一个包含值(从0到100)的数组,散点图中的每个点都有一个值:
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()

可用的颜色图
可以选择的内置颜色图(需要时再查)
尺寸
使用s参数更改点的大小。
就像颜色一样,确保大小数组的长度与x和y轴的数组长度相同:
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()

透明度
可以使用alpha参数调整点的透明度。
就像颜色一样,确保大小数组的长度与x和y轴的数组长度相同:
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()

组合颜色、大小和透明度
在点上组合具有不同大小的颜色图。
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()

柱状图
创建柱状图
在pyplot包中,使用bar()函数绘制柱状图:
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x, y)
plt.show()

bar()函数采用描述条形布局的参数。
类别及其值由第一个和第二个参数表示为数组。
x = ["APPLES", "BANANAS"]
y = [400, 350]
plt.bar(x, y)
<BarContainer object of 2 artists>

水平柱状图
如果柱状水平显示而不是垂直显示,请使用bath()函数:
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.barh(x, y)
plt.show()

柱状图颜色
bar()和barh()使用关键字参数color来设置颜色:
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x, y, color="red")
plt.show()

颜色名称
使用140种支持的颜色名称中的任何一种。
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x, y, color="hotpink")
plt.show()

十六进制颜色
或者可以使用十六进制颜色值:
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x, y, color="#4CAF50")
plt.show()

条形宽度
bar()使用关键字参数width来设置宽度:
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.bar(x, y, width=0.1)
plt.show()

条形宽度
barh()使用关键字参数height来设置条形的高度:
x = np.array(["A", "B", "C", "D"])
y = np.array([3, 8, 1, 10])
plt.barh(x, y, height=0.1)
plt.show()

创建直方图
在Matplotlib中,我们使用hist()函数来创建直方图。
hist()函数将使用一个数字数组来创建直方图,该数组作为参数发送到函数中。
为简单起见,我们使用Numpy随机生成一个包含250个值的数组,其中值将集中在170左右,标准差为10.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.normal(170, 10, 250) # 均值 方差 个数
print(x)
[175.58784035 170.67184328 166.86827665 168.18700544 174.41734808
172.57928064 163.9901554 175.5280239 182.06339974 178.26195651
177.69990499 162.39682091 181.60260582 167.27976691 157.53753994
168.85344733 169.58495002 162.53077861 167.93790021 182.18018689
165.03594017 175.02336695 176.34278735 169.05901716 177.16449407
170.75045619 169.11481702 157.68312955 171.31732556 190.05150642
179.49523314 200.0619179 178.37875646 176.69769634 167.20239139
176.43764106 170.26257312 171.63443689 176.54907018 171.13799638
184.31082395 164.32755859 153.33259684 177.29524536 168.36671954
174.77624448 168.28072704 169.28467141 160.98306841 158.48005571
165.89584313 176.11426403 177.09334915 152.41575753 155.94365027
169.57925355 167.64892386 177.9872364 163.88240388 175.93685093
183.75454903 173.64295341 175.78418014 164.178739 169.20065264
183.43195015 165.74629157 164.63195959 170.19723796 154.12762345
156.05430609 186.07481881 193.40418636 165.05660103 164.60364149
166.31032407 181.66534561 169.34064349 177.03887443 163.79593615
165.08915932 181.35319964 175.90443159 163.12102878 163.90230336
174.32630173 169.82177766 193.96154664 177.31423176 178.82134422
149.60141932 181.24175671 176.5130141 186.54303502 149.52048287
167.61914857 162.31103132 158.73441853 157.94293234 172.13503936
154.46565546 167.36390915 156.04514619 177.13971641 174.61868178
177.9204504 170.02768387 163.38682332 167.5907203 157.84107366
161.1692403 166.32666487 167.07862595 164.85586309 169.99821076
159.44382447 153.4993626 160.05318283 152.05508727 167.47995681
167.85584656 184.55963764 159.31154785 155.89563137 159.65478176
170.86504371 170.42371159 156.77080587 154.3772196 160.46266454
178.94096439 156.07284304 181.527611 169.89133097 178.91254188
158.5256148 158.3833149 174.11464975 177.5316297 164.00671951
163.44615564 170.47837447 179.97061825 175.21385726 163.08902006
168.07067165 164.77150163 175.49770308 159.63784364 161.45875224
186.10597692 178.12735711 160.32155978 165.10762035 177.65177954
158.1391917 171.18173754 178.15430473 171.09633401 189.7173402
178.89320793 141.65043585 178.12522711 167.22275179 186.85840469
175.28244741 170.99035279 162.27389217 160.86252832 170.46423197
176.12614914 179.41979885 178.89067062 169.90666146 191.18602446
157.17306531 178.82619187 189.53382397 177.71956431 156.60077511
185.0026245 177.14530099 168.83927126 161.4739883 174.06394525
161.90370627 170.44493613 146.47290372 169.00942979 196.07612984
166.69570997 174.11799918 167.17621336 180.54397688 184.03805936
188.90726963 170.80939681 171.97008292 166.83101754 160.32700512
167.72792992 148.7774827 165.57564539 178.54148335 176.08423831
154.30111672 169.90696191 179.81405828 160.45199874 174.87789907
192.85761202 170.91952503 157.49297175 168.40460711 175.16287626
168.46894944 187.235561 168.78197959 167.48119473 168.61676393
154.26407721 149.9691542 183.83197309 175.33838201 180.82613859
172.85908979 165.91860758 182.42544038 170.1040848 169.63188507
166.80472824 167.11604017 164.1238919 162.54208155 161.50742183
180.08166443 175.50010153 166.95460011 164.49152575 186.27022421
166.73943861 153.74352487 152.96735939 162.76346378 178.62267616
130.34425335 171.89435578 173.07666076 179.36892897 174.385056 ]
plt.hist(x)
plt.show()

饼图
创建饼图
在pyplot包中,可以使用ple()函数绘制饼图:
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
plt.pie(y)
plt.show()

标签
使用label参数为饼图添加标签。
label参数必须是一个数组,每个楔形都有一个标签:
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels=mylabels)
plt.show()

起始角度
如前所述,默认起始角度位于x轴,但可以通过指定startangle参数来更改起始角度。
startangle参数定义为以度为单位的角度,默认角度为0:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Tvr2qJw3-1659780917438)(attachment:image.png)]
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels=mylabels, startangle=90)
plt.show()

Explode
如果想让一个楔子脱颖而出,可以使用explode参数
如果制定了explode参数,而不是None,则必须是一个数组,每个楔形都有一个值
每个值表示每个楔形显示的距离中心多远:
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels=mylabels, explode=myexplode)
plt.show()

阴影
通过将shadows参数设置为True为饼图添加阴影:
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels=mylabels, explode=myexplode, shadow=True)
plt.show()

颜色
使用colors参数设置每个楔形的颜色
如果指定了颜色参数,则必须是一个数组,每个楔形都有一个值:
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["black", "hotpink", "b", "#4CAF50"] #b是一个简写 代表blue
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels=mylabels, explode=myexplode, shadow=True, colors=mycolors)
plt.show()

图例
要为每个楔形添加解释列表,请使用legend()函数:
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels=mylabels, explode=myexplode, shadow=True)
plt.legend()
plt.show()

带标题的图例
要向图例添加标题,请将标题参数添加到图例参数。
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels=mylabels, explode=myexplode, shadow=True)
plt.legend(title="Four Fruits:")
plt.show()

本文详细介绍了使用PyPlot在Matplotlib中绘制线图、散点图、柱状图、饼图等的基本技巧,包括线型、颜色、标记、标签和图例的设置,以及如何创建和定制复杂的图表。

2万+

被折叠的 条评论
为什么被折叠?



