如何在 seaborn 中显示所有数字图例值?
How to display all numeric legend values in seaborn?
我正在尝试为以下数据框创建 sns.lineplot()
:
overs:
season over total_runs total_overs avg_run
0 2008 1 703 745 0.943624
1 2008 2 923 741 1.245614
2 2008 3 826 727 1.136176
3 2008 4 912 725 1.257931
4 2008 5 1017 722 1.408587
235 2019 16 1099 721 1.524272
236 2019 17 1035 707 1.463932
237 2019 18 1124 695 1.617266
238 2019 19 1209 669 1.807175
239 2019 20 1189 552 2.153986
240 rows × 5 columns
sns.lineplot(x='avg_run', y='over', hue='season', data='overs')
我得到的输出如下:
- 整个赛季(范围:2008-2019)我都没有得到传奇,我无法区分当前
lineplots
。
- 请注意:我的要求是在同一张图中绘制所有线条
- 尝试为
seaborn.lineplot()
使用 legend='full'
参数
- 当图例值为数字时,这可能是必要的。
- 有不同的 color palettes 可供选择。
import pandas as pd
import numpy as np
import seaborn as sns
# test data
sample_length = range(1, 6+1)
rads = np.arange(0, 2*np.pi, 0.01)
data = np.array([np.sin(t*rads) for t in sample_length])
df = pd.DataFrame(data.T, index=pd.Series(rads.tolist(), name='radians'), columns=sample_length)
dfl = df.stack().reset_index().rename(columns={'level_1': 'frequency', 0: 'amplitude'})
# plot
sns.lineplot(x='radians', y='amplitude', hue='frequency', data=dfl, legend='full', palette='winter')
自定义颜色图
- Select 一个调色板,其中包含足够多的独特颜色以供绘图中的线条数使用。
- 可在
seaborn.husl_palette
找到 husl
调色板的其他选项
colors
也可以是手动选择颜色的列表
colors = ['red', 'blue', 'green', 'black', 'purple', 'yellow']
# create color mapping based on all unique values of frequency
freqs = dfl.frequency.unique()
colors = sns.color_palette('husl', n_colors=len(freqs)) # get a number of colors
cmap = dict(zip(freqs, colors)) # zip freqs to colors and create a dict
sns.lineplot(x='radians', y='amplitude', hue='frequency', data=dfl, legend='full', palette=cmap)
我正在尝试为以下数据框创建 sns.lineplot()
:
overs:
season over total_runs total_overs avg_run
0 2008 1 703 745 0.943624
1 2008 2 923 741 1.245614
2 2008 3 826 727 1.136176
3 2008 4 912 725 1.257931
4 2008 5 1017 722 1.408587
235 2019 16 1099 721 1.524272
236 2019 17 1035 707 1.463932
237 2019 18 1124 695 1.617266
238 2019 19 1209 669 1.807175
239 2019 20 1189 552 2.153986
240 rows × 5 columns
sns.lineplot(x='avg_run', y='over', hue='season', data='overs')
我得到的输出如下:
- 整个赛季(范围:2008-2019)我都没有得到传奇,我无法区分当前
lineplots
。 - 请注意:我的要求是在同一张图中绘制所有线条
- 尝试为
seaborn.lineplot()
使用legend='full'
参数- 当图例值为数字时,这可能是必要的。
- 有不同的 color palettes 可供选择。
import pandas as pd
import numpy as np
import seaborn as sns
# test data
sample_length = range(1, 6+1)
rads = np.arange(0, 2*np.pi, 0.01)
data = np.array([np.sin(t*rads) for t in sample_length])
df = pd.DataFrame(data.T, index=pd.Series(rads.tolist(), name='radians'), columns=sample_length)
dfl = df.stack().reset_index().rename(columns={'level_1': 'frequency', 0: 'amplitude'})
# plot
sns.lineplot(x='radians', y='amplitude', hue='frequency', data=dfl, legend='full', palette='winter')
自定义颜色图
- Select 一个调色板,其中包含足够多的独特颜色以供绘图中的线条数使用。
- 可在
seaborn.husl_palette
找到 colors
也可以是手动选择颜色的列表colors = ['red', 'blue', 'green', 'black', 'purple', 'yellow']
husl
调色板的其他选项
# create color mapping based on all unique values of frequency
freqs = dfl.frequency.unique()
colors = sns.color_palette('husl', n_colors=len(freqs)) # get a number of colors
cmap = dict(zip(freqs, colors)) # zip freqs to colors and create a dict
sns.lineplot(x='radians', y='amplitude', hue='frequency', data=dfl, legend='full', palette=cmap)