使用seaborn / matplotlib箱图时的刻度频率 [英] tick frequency when using seaborn/matplotlib boxplot

查看:247
本文介绍了使用seaborn / matplotlib箱图时的刻度频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用seaborn绘制一系列的箱型图,

  sns.boxplot(full_array)

其中 full_array 包含200个数组。
因此,我在0到200的x轴上有200个框线和刻度。



xticks彼此之间距离太近,我想仅显示其中一些,例如,每20个左右会显示一个标记的xtick。



我尝试了几种解决方案,如提到的


I am plotting with seaborn a series of boxplots with

sns.boxplot(full_array)

where full_array contains 200 arrays. Therefore, I have 200 boxplots and ticks on the x-axis from 0 to 200.

The xticks are too close to each other and I would like to show only some of them, for instance, a labeled xtick every 20, or so.

I tried several solutions as those mentioned here but they did not work.

Every time I sample the xticks, I get wrong labels for the ticks, as they get numbered from 0 to N, with unit spacing.

For instance, with the line ax.xaxis.set_major_locator(ticker.MultipleLocator(20)) I get a labelled xtick every 20 but the labels are 1, 2, 3, 4 instead of 20, 40, 60, 80...

Thanks to anyone who's so kind to help.

解决方案

The seaborn boxplot uses a FixedLocator and a FixedFormatter, i.e.

print ax.xaxis.get_major_locator()
print ax.xaxis.get_major_formatter()

prints

<matplotlib.ticker.FixedLocator object at 0x000000001FE0D668>
<matplotlib.ticker.FixedFormatter object at 0x000000001FD67B00>

It's therefore not sufficient to set the locator to a MultipleLocator since the ticks' values would still be set by the fixed formatter.

Instead you would want to set a ScalarFormatter, which sets the ticklabels to correspond to the numbers at their position.

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn.apionly as sns
import numpy as np

ax = sns.boxplot(data = np.random.rand(20,30))

ax.xaxis.set_major_locator(ticker.MultipleLocator(5))
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())

plt.show()

这篇关于使用seaborn / matplotlib箱图时的刻度频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆