我正在与Seaborn一起绘制一系列的拳击表

问题描述 投票:0回答:1
Xticks彼此之间太近了,我只想显示其中一些标记的XTICK每20个标记XTICK。

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

here
,但它们没有起作用。
每次我品尝Xticks,我的tick刻有错误的标签,因为它们的数量从0到n,并带有单位间距。
,例如,线

ax.xaxis.set_major_locator(ticker.MultipleLocator(20))

我每20次获得标签Xtick,但标签为1、2、3、4,而不是20、40、60、80 ...

海洋杂货店使用固定插座和固定形式,即 print ax.xaxis.get_major_locator() print ax.xaxis.get_major_formatter()

印花

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

因此,这不足以将定位器设置为

MultipleLocator
,因为tick的值仍将由固定的格式设置。

INSTEAD您要设置一个
ScalarFormatter

,将tick标签设置为与其位置的数字相对应。

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()
python matplotlib seaborn boxplot xticks
1个回答
41
投票


    

在Seaborn的现代版本(v.0.12.0等)中,tick的值由
FuncFormatter

设置,因此OP的初始尝试正常。

import numpy as np
import seaborn as sns
from matplotlib.ticker import MultipleLocator

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

ax.xaxis.get_major_locator()                    # <matplotlib.ticker.FixedLocator at 0x2221f657340>
ax.xaxis.get_major_formatter()                  # <matplotlib.ticker.FuncFormatter at 0x2221f8e2a00>

ax.xaxis.set_major_locator(MultipleLocator(5))  # show every 5th tick

有效的方法是简单地通过选择每5个来设置XTICK。

ax = sns.boxplot(data=data); ax.set_xticks(ax.get_xticks()[::5]); # show every 5th tick

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.