每个数据点的散点图与标记的大小和颜色相匹配
Scatter plot with different text at each data point that matches the size and colour of the marker
我有这个散点图(我知道它很乱!),我正在尝试更改与标记相邻的文本的颜色和大小以匹配标记的颜色和大小。在这种情况下,绿点旁边的文本将为绿色,而橙色点旁边的文本将为橙色。理想情况下,我还可以缩小文本。
我用来生成下面散点图的代码是:
plot = plt.figure(figsize=(30,20))
ax = sns.scatterplot(x='Recipients', y='Donors', data=concatenated, hue = 'Cost of Transfer',
palette="Set2", s= 300)
def label_point(x, y, val, ax):
a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
for i, point in a.iterrows():
ax.text(point['x']+.1, point['y'], str(point['val']))
label_point(concatenated.Recipients, concatenated.Donors, concatenated.Species, plt.gca())
非常感谢任何帮助:)
绘图中的文本由 ax.text()、matplotlib axes.text.
设置
# Before
ax.text(point['x']+.1, point['y'], str(point['val']))
# After
ax.text(point['x']+.1, point['y'], str(point['val']), {'color': 'g', 'fontsize': 20})
试试你喜欢的颜色和字体大小。
尝试在 sns.scatterplot()
中找到点的颜色会非常复杂并且可能容易出错。您真的需要使用 scatterplot()
吗?
如果没有,我会建议忘记 seaborn 并直接使用 matplotlib 创建绘图,这给了你更多的控制权:
iris = sns.load_dataset("iris")
iris['label'] = 'label_'+iris.index.astype(str) # create a label for each point
df = iris
x_col = 'sepal_length'
y_col = 'sepal_width'
hue_col = 'species'
label_col = 'label'
palette = 'Set2'
size = 5
fig, ax = plt.subplots()
colors = matplotlib.cm.get_cmap(palette)(range(len(df[hue_col].unique())))
for (g,temp),c in zip(iris.groupby('species'),colors):
print(g,c)
ax.plot(temp[x_col], temp[y_col], 'o', color=c, ms=size, label=g)
for i,row in temp.iterrows():
ax.annotate(row[label_col], xy=(row[x_col],row[y_col]), color=c)
ax.set_xlabel(x_col)
ax.set_ylabel(y_col)
ax.legend(title=hue_col)
我有这个散点图(我知道它很乱!),我正在尝试更改与标记相邻的文本的颜色和大小以匹配标记的颜色和大小。在这种情况下,绿点旁边的文本将为绿色,而橙色点旁边的文本将为橙色。理想情况下,我还可以缩小文本。
我用来生成下面散点图的代码是:
plot = plt.figure(figsize=(30,20))
ax = sns.scatterplot(x='Recipients', y='Donors', data=concatenated, hue = 'Cost of Transfer',
palette="Set2", s= 300)
def label_point(x, y, val, ax):
a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
for i, point in a.iterrows():
ax.text(point['x']+.1, point['y'], str(point['val']))
label_point(concatenated.Recipients, concatenated.Donors, concatenated.Species, plt.gca())
非常感谢任何帮助:)
绘图中的文本由 ax.text()、matplotlib axes.text.
设置# Before
ax.text(point['x']+.1, point['y'], str(point['val']))
# After
ax.text(point['x']+.1, point['y'], str(point['val']), {'color': 'g', 'fontsize': 20})
试试你喜欢的颜色和字体大小。
尝试在 sns.scatterplot()
中找到点的颜色会非常复杂并且可能容易出错。您真的需要使用 scatterplot()
吗?
如果没有,我会建议忘记 seaborn 并直接使用 matplotlib 创建绘图,这给了你更多的控制权:
iris = sns.load_dataset("iris")
iris['label'] = 'label_'+iris.index.astype(str) # create a label for each point
df = iris
x_col = 'sepal_length'
y_col = 'sepal_width'
hue_col = 'species'
label_col = 'label'
palette = 'Set2'
size = 5
fig, ax = plt.subplots()
colors = matplotlib.cm.get_cmap(palette)(range(len(df[hue_col].unique())))
for (g,temp),c in zip(iris.groupby('species'),colors):
print(g,c)
ax.plot(temp[x_col], temp[y_col], 'o', color=c, ms=size, label=g)
for i,row in temp.iterrows():
ax.annotate(row[label_col], xy=(row[x_col],row[y_col]), color=c)
ax.set_xlabel(x_col)
ax.set_ylabel(y_col)
ax.legend(title=hue_col)