簡體   English   中英

seaborn 線圖為標記設置透明度

[英]seaborn line plot set transparency for markers

如何在seaborn.lineplot分別設置標記和線條的seaborn.lineplot

我有一組點,我想繪制一個連接所有點的線圖。 我希望線條比標記更透明。 怎么做?

這是我的目標: 在此處輸入圖片說明

這是我的代碼:

import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

matplotlib.rcParams['legend.handlelength'] = 0
matplotlib.rcParams['legend.numpoints'] = 1

data = [{'method': 'end-to-end', 'k': 1, 'time': 181.0, 'auc': 69.76}, {'method': 'end-to-end', 'k': 2, 'time': 193.0, 'auc': 71.12}, {'method': 'end-to-end', 'k': 3, 'time': 256.0, 'auc': 71.84}, {'method': 'end-to-end', 'k': 4, 'time': 302.0, 'auc': 71.87}, {'method': 'end-to-end', 'k': 5, 'time': 365.0, 'auc': 71.89}, {'method': 'end-to-end', 'k': 8, 'time': 602.0, 'auc': 71.87}, {'method': 'end-to-end', 'k': 10, 'time': 743.0, 'auc': 71.84}, {'method': 'first', 'k': 1, 'time': 82.0, 'auc': 69.01}, {'method': 'first', 'k': 2, 'time': 105.0, 'auc': 69.45}, {'method': 'first', 'k': 3, 'time': 171.0, 'auc': 70.11}, {'method': 'first', 'k': 4, 'time': 224.0, 'auc': 70.36}, {'method': 'first', 'k': 5, 'time': 279.0, 'auc': 70.74}, {'method': 'first', 'k': 8, 'time': 517.0, 'auc': 70.81}, {'method': 'first', 'k': 10, 'time': 654.0, 'auc': 70.98}]

data = pd.DataFrame(data)
g = sns.lineplot(x='time', y='auc', data=data, markers=['o','v'], markersize=7, sort=False, hue='method', style="method", dashes=False, linestyle=None)
plt.legend(loc='lower right')

Seaborn 似乎使顏色更淺。 您可以遍歷生成的點並更改它們的亮度。 (點存儲在ax.collections 。)亮度范圍從0 (黑色)到1 (白色)。

由於現在點和線的顏色不同,並且在點的頂部繪制了線,結果看起來有點奇怪。 您可以更改線條的zorder以將它們移到后面。

應該再次創建圖例(通過ax.legend )以顯示新的標記顏色。

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

sns.set_style('darkgrid')
flights = sns.load_dataset('flights')
## markers = [".",",","o","v","^","<",">","1","2","3","4","8","s","p","P","*","h","H","+","x","X","D","d","|","_",0,1,2,3,4,5,6,7,8,9,10,11]
markers = ["o", "v", "^", "<", ">", "8", "s", "*", "h", "H", "P", "p"]
ax = sns.pointplot(data=flights, x='year', y='passengers', hue='month', markers=markers, palette='Set3')
for dots in ax.collections:
    color = dots.get_facecolor()
    dots.set_color(sns.set_hls_values(color, l=0.5))
    dots.set_alpha(1)
for line in ax.lines:
    line.set_zorder(0)
ax.legend(bbox_to_anchor=(1.01, 1.02), loc='upper left')
plt.tight_layout()
plt.show()

sns.lineplot 帶有較深的標記顏色

您可以嘗試以下操作:

# ...
alpha = 0.25

g = sns.lineplot(x='time', y='auc', data=data, markers=['o','v'], markersize=7, 
                 sort=False, hue='method', style="method", dashes=False, 
                 linestyle=None)
for line in g.lines:
    color = line.get_color()  # get the color tuple of the line
    line.set_markerfacecolor(color)  # set the marker color explicitly
    line.set_color(color + (alpha,))  # set the line transparent
plt.legend(loc='lower right')

輸出:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM