seaborn 線圖為標記設置透明度

命名空間-Pt

如何分別設置標記和線條的透明度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')

約翰·C

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 帶有較深的標記顏色

#更新

上面的答案是在您添加代碼之前編寫的。對於代碼中的示例,您可以調用lineplot兩次。(注意,'axes level functions' 返回 an ax,而'figure level functions'返回一個 seaborn grid-like 數據結構,例如 a FacetGrid。因此,a custom is only use gfor the return value of figure-level functions,以及類似於ax軸級函數的名稱。這樣的命名可以更容易地了解如何應用現有的示例代碼。)

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)
sns.set_style('ticks')

ax = sns.lineplot(x='time', y='auc', data=data, markers=['o','v'], markersize=7, sort=False, hue='method', style='method',  dashes=False, linestyle='', alpha=1)
sns.lineplot(x='time', y='auc', data=data, sort=False, hue='method',   dashes=False,  alpha=0.3, legend=False, ax=ax)
ax.legend(loc='lower right')
plt.show()

兩次 sns.lineplot

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章