python - 如何使用 matplotlib/seabornn 将边缘线添加到图例上的标记
问题描述
我有一个带有图例的气泡图,显示了按颜色和大小区分的两个类别,散点图上的标记有黑色边缘颜色,我喜欢图例中显示的标记也有它,有人知道我该怎么做吗?
我正在使用 seaborn 散点图来生成图表
Summary = pd.read_csv("Summary.csv")
####################################################################################
# Plot the scatterplot
####################################################################################
f, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, sharey=True, figsize=(8.27,5.8))
#----------------------------------------------------------------------------------------
# add a big axes, hide frame, common axis labels
#----------------------------------------------------------------------------------------
f.add_subplot(111, frameon=False)
# hide tick and tick label of the big axes
plt.tick_params(labelcolor='none', top=False, bottom=False, left=False, right=False)
plt.grid(False)
plt.xlabel('T$_{s}$ - T$_{sat}$ [$^{\circ}$C]', fontsize=14)
plt.ylabel('Heat Flux [W$\cdot$cm$^{-2}$]', fontsize=14)
#----------------------------------------------------------------------------------------
# Change the minimum and maximum point size
ax=sns.scatterplot(x='T_surf - T_sat [C]', y='Heat Flux [W/cm2]', data=Summary, # Choose the x and y values and the dataframe
size='Mass flux [kg$\cdot$m$^{-2}$$\cdot$s$^{-1}$]', # Size of markers
hue='Enhanced Surface', # Color by surface type
#color='Greys',
alpha=1.0,
#style='Enhanced Surface', # Marker by surface type
sizes=(20, 700), # minimum and maximum marker size
palette = 'Blues',
#markers=["X","d", "*","s"], # Change the markers' style
markers='o',
edgecolor= "Black", # Change the edge color of the markers
linewidth=0.5, # Change the edge linewidth of the markers
legend=False,
ax=ax1
)
ax=sns.scatterplot(x='T_surf - T_sat [C]', y='Heat Flux [W/cm2]', data=Summary, # Choose the x and y values and the dataframe
size='Mass flux [kg$\cdot$m$^{-2}$$\cdot$s$^{-1}$]', # Size of markers by population
hue='Enhanced Surface', # Color by surface type
#cmap='Greys',
alpha=1.0,
#style='Enhanced Surface', # Marker by surface type
sizes=(20, 700), # minimum and maximum marker size
palette = 'Blues',
#markers=["X","d", "*","s"], # Change the markers' style
markers='o',
edgecolor= "Black", # Change the edge color of the markers
linewidth=0.5, # Change the edge linewidth of the markers
legend='auto',
ax=ax2
)
ax1.set_xlim(-60, 120)
ax2.set_xlim(790, 810)
# labels = ax1.get_legend_handles_labels()
####################################################################################
# Use the matplotlib library to edit the plot
####################################################################################
#Legend
lgd = ax2.legend(loc="upper right", frameon = 0.5, framealpha=0.8, # Create a legend and define its location, frame and frame alpha
edgecolor='white', facecolor='white', ncol=2, # Edgecolor, facecolor and the number of columns
title='', fontsize=10, # the title and the font size
handlelength=1, handleheight=2, columnspacing = 1.0, labelspacing=1.5,
markerscale=1.0, markerfirst = False)
解决方案
您可以按照自己的方式创建情节seaborn
,然后直接调整图例样式。
每个ha
手柄都是一个matplotlib.collections.PathCollection
,我已将边缘颜色设置为red
此处,以便您可以看到差异。
import seaborn as sns
tips = sns.load_dataset("tips")
fig, ax = plt.subplots(figsize=(4,4))
sns.scatterplot(data=tips, x="total_bill", y="tip", style="day", ax=ax)
# Get the legend handles
handles, labels = ax.get_legend_handles_labels()
# Iterate through the handles and call `set_edgecolor` on each
for ha in handles:
ha.set_edgecolor("red")
# Use `ax.legend` to set the modified handles and labels
lgd = ax.legend(
handles,
labels,
loc="upper left",
ncol=2,
)
产生:
或者,正如mwaskom建议的那样,修改艺术家以避免重绘图例:
tips = sns.load_dataset("tips")
fig, ax = plt.subplots(figsize=(4,4))
sns.scatterplot(data=tips, x="total_bill", y="tip", style="day", ax=ax)
# Place the legend
lgd = ax.legend(
loc="upper left",
ncol=2,
)
# Modify the point edge colour
for ha in ax.legend_.legendHandles:
ha.set_edgecolor("red")
产生相同的情节。
推荐阅读
- html-table - 根据与 Cypress 同一行的单元格内容断言单元格内容
- javascript - 在jQuery中的多个li之间插入HTML元素
- cmake - 对 add_library() 感到困惑
- sql-server - 如果不存在 SQL Server 插入 - 没有主键
- c# - 突出显示激活的按钮 WPF
- excel - 在 VBA 中为具有内部引用的 UDF 设置求解器
- mysql - MariaDB如何获取子查询结果的第一条记录
- python - 将小图像作为 numpy 数组添加到画布
- oracle-sqldeveloper - 如何编辑 SQL-Developer 的导出模板
- numpy - np.ma.average() 以避免除零错误