Seaborn 小提琴图未按正确计数缩放
Seaborn violin plot not scaling by count correctly
我试图通过计数来缩放我的小提琴图,但是最后三个小提琴,每个都基于三个数据点,比前三个大得多,前三个基于更多。
我的代码如下:
fig = plt.figure(figsize=(20,10))
grid = plt.GridSpec(1, 1, wspace=0.15, hspace=0)
plotol= fig.add_subplot(grid[0,0])
olivine = sns.violinplot(x=olivinedata.Sample, y=olivinedata.FoContent, scale='count', hue=olivinedata.RimCore, order=["85B", "95B", "98", "LZa* (Tranquil)", "LZa* (Banded)", "LZb* ", "LZa", "LZb", "LZc"], ax=plotol)
plotol.set_xticklabels(plotol.get_xticklabels(),
rotation=20, fontsize = 15,
horizontalalignment='right')
plotol.set_yticklabels(plotol.get_yticks(), size=15)
plotol.set_xlabel("Sample",size = 24,alpha=0.7)
plotol.set_ylabel("Fo# (mol. %)",size = 24,alpha=0.7)
plt.setp(plotol.get_legend().get_texts(), fontsize='22')
plotol.legend(title="Measurement Type")
我也收到警告消息
用户警告:FixedFormatter 只能与 FixedLocator 一起使用
如果 sys.path[0] == '':
这是包含以下行的结果:
plotol.set_yticklabels(plotol.get_yticks(), size=15)
我不知道为什么。感谢您的帮助!
您可能需要 scale_hue=False
,否则按 x 类别进行缩放。
这里是 scale
选项的比较,有和没有 scale_hue
:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
df1 = pd.DataFrame({'sample': np.repeat([*'ABC'], 20),
'hue': np.repeat([*'BBRBRB'], 10),
'val': np.random.uniform(10, 20, 60)})
df2 = pd.DataFrame({'sample': np.repeat([*'XYZ'], 3),
'hue': np.repeat([*'BBB'], 3),
'val': np.random.uniform(10, 20, 9)})
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(24, 8))
for row, scale_hue in zip([0, 1], [True, False]):
for ax, scale in zip(axes[row, :], ['area', 'count', 'width']):
sns.violinplot(data=pd.concat([df1, df2]), x='sample', y='val', hue='hue',
scale=scale, scale_hue=scale_hue, ax=ax)
ax.set_title(f"scale='{scale}', scale_hue={scale_hue}", size=16)
plt.tight_layout()
plt.show()
我试图通过计数来缩放我的小提琴图,但是最后三个小提琴,每个都基于三个数据点,比前三个大得多,前三个基于更多。
我的代码如下:
fig = plt.figure(figsize=(20,10))
grid = plt.GridSpec(1, 1, wspace=0.15, hspace=0)
plotol= fig.add_subplot(grid[0,0])
olivine = sns.violinplot(x=olivinedata.Sample, y=olivinedata.FoContent, scale='count', hue=olivinedata.RimCore, order=["85B", "95B", "98", "LZa* (Tranquil)", "LZa* (Banded)", "LZb* ", "LZa", "LZb", "LZc"], ax=plotol)
plotol.set_xticklabels(plotol.get_xticklabels(),
rotation=20, fontsize = 15,
horizontalalignment='right')
plotol.set_yticklabels(plotol.get_yticks(), size=15)
plotol.set_xlabel("Sample",size = 24,alpha=0.7)
plotol.set_ylabel("Fo# (mol. %)",size = 24,alpha=0.7)
plt.setp(plotol.get_legend().get_texts(), fontsize='22')
plotol.legend(title="Measurement Type")
我也收到警告消息
用户警告:FixedFormatter 只能与 FixedLocator 一起使用 如果 sys.path[0] == '':
这是包含以下行的结果:
plotol.set_yticklabels(plotol.get_yticks(), size=15)
我不知道为什么。感谢您的帮助!
您可能需要 scale_hue=False
,否则按 x 类别进行缩放。
这里是 scale
选项的比较,有和没有 scale_hue
:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
df1 = pd.DataFrame({'sample': np.repeat([*'ABC'], 20),
'hue': np.repeat([*'BBRBRB'], 10),
'val': np.random.uniform(10, 20, 60)})
df2 = pd.DataFrame({'sample': np.repeat([*'XYZ'], 3),
'hue': np.repeat([*'BBB'], 3),
'val': np.random.uniform(10, 20, 9)})
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(24, 8))
for row, scale_hue in zip([0, 1], [True, False]):
for ax, scale in zip(axes[row, :], ['area', 'count', 'width']):
sns.violinplot(data=pd.concat([df1, df2]), x='sample', y='val', hue='hue',
scale=scale, scale_hue=scale_hue, ax=ax)
ax.set_title(f"scale='{scale}', scale_hue={scale_hue}", size=16)
plt.tight_layout()
plt.show()