seaborn 线图设置标记的透明度
seaborn line plot set transparency for markers
如何在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()
#更新
以上答案是在您添加代码之前写的。对于代码中的示例,您可以调用 lineplot
两次。 (注意 'axes level functions' return 一个 ax
,而 'figure level functions' return 一个 seaborn 网格状数据结构,例如 FacetGrid
。因此,一个习惯上就是图级函数的return值的名字只用g
,轴级函数的名字类似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()
如何在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()
#更新
以上答案是在您添加代码之前写的。对于代码中的示例,您可以调用 lineplot
两次。 (注意 'axes level functions' return 一个 ax
,而 'figure level functions' return 一个 seaborn 网格状数据结构,例如 FacetGrid
。因此,一个习惯上就是图级函数的return值的名字只用g
,轴级函数的名字类似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()