多种颜色填充 matplotlib 标记
Multiple color fills in matplotlib markers
我想在用 matplotlib 制作的标记中使用多种颜色。按照 this example, and with some additional info from this documentation,做两种颜色并不难。但是,我想知道是否可以制作超过 2 种颜色的标记。我处于一种情况,我希望单个标记实际上获得 3 种不同的颜色(地图上的一个点指的是三个不同的观察结果)。
您可以按照此处显示的 matplotlib 示例执行此操作:
https://matplotlib.org/stable/gallery/lines_bars_and_markers/scatter_piecharts.html
下面我稍微更改了示例以使用 ax.plot
而不是 ax.scatter
。
基本上这意味着您的所有标记必须具有相同的大小,而不是对 scatter
使用 s
kwarg,您使用 ms
(或 markersize
) kwarg plot
.
此外,您需要定义 markerfacecolor
.
而不是 facecolor
除这些变化外,其他一切都与原始示例相同。
"""
This example makes custom 'pie charts' as the markers for a scatter plot
Thanks to Manuel Metz for the example
"""
import numpy as np
import matplotlib.pyplot as plt
# first define the ratios
r1 = 0.2 # 20%
r2 = r1 + 0.4 # 40%
# define some sizes of the scatter marker
sizes = np.array([60, 80, 120])
# calculate the points of the first pie marker
#
# these are just the origin (0,0) +
# some points on a circle cos,sin
x1 = np.cos(2 * np.pi * np.linspace(0, r1))
y1 = np.sin(2 * np.pi * np.linspace(0, r1))
xy1 = np.row_stack([[0, 0], np.column_stack([x1, y1])])
s1 = np.abs(xy1).max()
x2 = np.cos(2 * np.pi * np.linspace(r1, r2))
y2 = np.sin(2 * np.pi * np.linspace(r1, r2))
xy2 = np.row_stack([[0, 0], np.column_stack([x2, y2])])
s2 = np.abs(xy2).max()
x3 = np.cos(2 * np.pi * np.linspace(r2, 1))
y3 = np.sin(2 * np.pi * np.linspace(r2, 1))
xy3 = np.row_stack([[0, 0], np.column_stack([x3, y3])])
s3 = np.abs(xy3).max()
fig, ax = plt.subplots()
# Here's where I made changes
ax.plot(np.arange(3), np.arange(3), marker=xy1,
ms=20, markerfacecolor='blue', markeredgecolor='None', linestyle='None') # I changed this line
ax.plot(np.arange(3), np.arange(3), marker=xy2,
ms=20, markerfacecolor='green', markeredgecolor='None', linestyle='None') # I changed this line
ax.plot(np.arange(3), np.arange(3), marker=xy3,
ms=20, markerfacecolor='red', markeredgecolor='None', linestyle='None') # I changed this line
plt.margins(0.05)
plt.show()
我想在用 matplotlib 制作的标记中使用多种颜色。按照 this example, and with some additional info from this documentation,做两种颜色并不难。但是,我想知道是否可以制作超过 2 种颜色的标记。我处于一种情况,我希望单个标记实际上获得 3 种不同的颜色(地图上的一个点指的是三个不同的观察结果)。
您可以按照此处显示的 matplotlib 示例执行此操作:
https://matplotlib.org/stable/gallery/lines_bars_and_markers/scatter_piecharts.html
下面我稍微更改了示例以使用 ax.plot
而不是 ax.scatter
。
基本上这意味着您的所有标记必须具有相同的大小,而不是对 scatter
使用 s
kwarg,您使用 ms
(或 markersize
) kwarg plot
.
此外,您需要定义 markerfacecolor
.
facecolor
除这些变化外,其他一切都与原始示例相同。
"""
This example makes custom 'pie charts' as the markers for a scatter plot
Thanks to Manuel Metz for the example
"""
import numpy as np
import matplotlib.pyplot as plt
# first define the ratios
r1 = 0.2 # 20%
r2 = r1 + 0.4 # 40%
# define some sizes of the scatter marker
sizes = np.array([60, 80, 120])
# calculate the points of the first pie marker
#
# these are just the origin (0,0) +
# some points on a circle cos,sin
x1 = np.cos(2 * np.pi * np.linspace(0, r1))
y1 = np.sin(2 * np.pi * np.linspace(0, r1))
xy1 = np.row_stack([[0, 0], np.column_stack([x1, y1])])
s1 = np.abs(xy1).max()
x2 = np.cos(2 * np.pi * np.linspace(r1, r2))
y2 = np.sin(2 * np.pi * np.linspace(r1, r2))
xy2 = np.row_stack([[0, 0], np.column_stack([x2, y2])])
s2 = np.abs(xy2).max()
x3 = np.cos(2 * np.pi * np.linspace(r2, 1))
y3 = np.sin(2 * np.pi * np.linspace(r2, 1))
xy3 = np.row_stack([[0, 0], np.column_stack([x3, y3])])
s3 = np.abs(xy3).max()
fig, ax = plt.subplots()
# Here's where I made changes
ax.plot(np.arange(3), np.arange(3), marker=xy1,
ms=20, markerfacecolor='blue', markeredgecolor='None', linestyle='None') # I changed this line
ax.plot(np.arange(3), np.arange(3), marker=xy2,
ms=20, markerfacecolor='green', markeredgecolor='None', linestyle='None') # I changed this line
ax.plot(np.arange(3), np.arange(3), marker=xy3,
ms=20, markerfacecolor='red', markeredgecolor='None', linestyle='None') # I changed this line
plt.margins(0.05)
plt.show()