matplotlib violinplot未按计数正确缩放

jdgnovmf  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(124)

我试图通过计数来缩放我的小提琴图,但最后三把小提琴,每把都基于三个数据点,比前三把大得多,前三把基于更多数据点。
代码如下:

  1. fig = plt.figure(figsize=(20,10))
  2. grid = plt.GridSpec(1, 1, wspace=0.15, hspace=0)
  3. plotol= fig.add_subplot(grid[0,0])
  4. 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)
  5. plotol.set_xticklabels(plotol.get_xticklabels(),
  6. rotation=20, fontsize = 15,
  7. horizontalalignment='right')
  8. plotol.set_yticklabels(plotol.get_yticks(), size=15)
  9. plotol.set_xlabel("Sample",size = 24,alpha=0.7)
  10. plotol.set_ylabel("Fo# (mol. %)",size = 24,alpha=0.7)
  11. plt.setp(plotol.get_legend().get_texts(), fontsize='22')
  12. plotol.legend(title="Measurement Type")

我还收到一条警告信息

  • 用户警告:如果sys.path[0] =='':*,则FixedFormatter应仅与FixedLocator一起使用

这是因为包含了以下行:

  1. plotol.set_yticklabels(plotol.get_yticks(), size=15)

我也不知道为什么任何帮助都很感激!

ugmeyewa

ugmeyewa1#

您可能需要scale_hue=False,否则缩放将按x类别进行。
下面是scale选项的比较,有和没有scale_hue

  1. import matplotlib.pyplot as plt
  2. import pandas as pd
  3. import numpy as np
  4. import seaborn as sns
  5. df1 = pd.DataFrame({'sample': np.repeat([*'ABC'], 20),
  6. 'hue': np.repeat([*'BBRBRB'], 10),
  7. 'val': np.random.uniform(10, 20, 60)})
  8. df2 = pd.DataFrame({'sample': np.repeat([*'XYZ'], 3),
  9. 'hue': np.repeat([*'BBB'], 3),
  10. 'val': np.random.uniform(10, 20, 9)})
  11. fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(24, 8))
  12. for row, scale_hue in zip([0, 1], [True, False]):
  13. for ax, scale in zip(axes[row, :], ['area', 'count', 'width']):
  14. sns.violinplot(data=pd.concat([df1, df2]), x='sample', y='val', hue='hue',
  15. scale=scale, scale_hue=scale_hue, ax=ax)
  16. ax.set_title(f"scale='{scale}', scale_hue={scale_hue}", size=16)
  17. plt.tight_layout()
  18. plt.show()

展开查看全部

相关问题