tick frequency when using seaborn/matplotlib boxplot

前端 未结 1 907
陌清茗
陌清茗 2020-12-24 15:25

I am plotting with seaborn a series of boxplots with

sns.boxplot(full_array)

where full_array contains 200 arrays. Therefore,

相关标签:
1条回答
  • 2020-12-24 15:41

    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()
    

    0 讨论(0)
提交回复
热议问题