使用 'size' 参数时调整散点图中的标记大小
Adjusting Marker Size in Scatter Plot when using 'size' argument
假设我正在 matplotlib 中的极坐标投影上创建散点图。我使用 size 参数将标记大小分配给一个变量。在生成的图上,标记非常小,有些几乎看不见。我还需要一个图例来显示不同大小标记的实际值。所以我需要增加标记大小但不更改图例中的标签(因此 s=3*t 不起作用,因为它更改了图例中的标签)。执行此操作的最佳方法是什么?
fig = plt.figure(figsize=(5,5))
plt.style.use('default')
ax = fig.add_subplot(projection='polar')
c = ax.scatter(r, s, c=c, s=t, cmap='copper')
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
plt.legend(*c.legend_elements('sizes', num=3), loc=(1, 0.8))
plt.show()
在此示例中,r、s、c 和 t 是简单的一维数组
使用 official sample,我自定义了图例标签。我得到了图例和标签,并重写了标签的值。
可能有一种方法可以从乳胶格式中获取值,但我一直没能找到。
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
# Compute areas and colors
N = 150
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r**2
colors = theta
fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
# ax.legend(*c.legend_elements('sizes', num=3), loc=(1, 0.8))
handlers, labels = c.legend_elements('sizes', num=3)
labels[0] = r'$\mathdefault{50}$'
labels[1] = r'$\mathdefault{75}$'
ax.legend(handlers, labels, loc=(1, 0.8))
plt.show()
修改前的图
假设我正在 matplotlib 中的极坐标投影上创建散点图。我使用 size 参数将标记大小分配给一个变量。在生成的图上,标记非常小,有些几乎看不见。我还需要一个图例来显示不同大小标记的实际值。所以我需要增加标记大小但不更改图例中的标签(因此 s=3*t 不起作用,因为它更改了图例中的标签)。执行此操作的最佳方法是什么?
fig = plt.figure(figsize=(5,5))
plt.style.use('default')
ax = fig.add_subplot(projection='polar')
c = ax.scatter(r, s, c=c, s=t, cmap='copper')
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
plt.legend(*c.legend_elements('sizes', num=3), loc=(1, 0.8))
plt.show()
在此示例中,r、s、c 和 t 是简单的一维数组
使用 official sample,我自定义了图例标签。我得到了图例和标签,并重写了标签的值。 可能有一种方法可以从乳胶格式中获取值,但我一直没能找到。
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
# Compute areas and colors
N = 150
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r**2
colors = theta
fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
# ax.legend(*c.legend_elements('sizes', num=3), loc=(1, 0.8))
handlers, labels = c.legend_elements('sizes', num=3)
labels[0] = r'$\mathdefault{50}$'
labels[1] = r'$\mathdefault{75}$'
ax.legend(handlers, labels, loc=(1, 0.8))
plt.show()
修改前的图