【问题标题】:Matplotlib tick dirrection for different sides of boxplot箱线图不同侧面的 Matplotlib 刻度方向
【发布时间】:2016-03-16 23:07:27
【问题描述】:

在箱线图的左侧和右侧设置刻度方向的方法是什么? 当我x_or_y_axis.set_tick_params(direction='whatever') 时,它会改变框两侧刻度的方向:

  _______________        _______________
-|               |-     |-             -|
 |               |      |-             -|
-|               |-     |-             -|
 |               |      |-             -|
-|_______________|-  or |_______________|

但我需要:

  _______________         _______________
 |-              |-     -|             -|
 |               |       |              |
 |-              |-     -|             -|
 |               |       |              |
 |-______________|-  or -|_____________-|

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    一种选择是使用ax.twinx() 创建一个双轴,然后您可以分别控制两个轴上的yticks

    您可能还希望在两个轴之间共享 y 轴,以便两个轴之间的轴范围和刻度相同。我们可以使用答案here 来做到这一点。

    这是一个最小的例子,从boxplot demo修改:

    import matplotlib.pyplot as plt
    import numpy as np
    
    # fake up some data
    spread = np.random.rand(50) * 100
    center = np.ones(25) * 50
    flier_high = np.random.rand(10) * 100 + 100
    flier_low = np.random.rand(10) * -100
    data = np.concatenate((spread, center, flier_high, flier_low), 0)
    
    # Make the figure and axes
    fig,ax = plt.subplots(1)
    
    # Add you twin axes
    ax2 = ax.twinx()
    
    # Set the ticks to the outside on the right only
    ax2.tick_params(axis='y',direction='out')
    
    # Make sure the ticks and axes limits are shared between the left and right
    ax.get_shared_y_axes().join(ax,ax2)
    
    # basic boxplot
    ax.boxplot(data)
    
    plt.show()
    

    【讨论】:

    • 谢谢你是我的救星!
    猜你喜欢
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 2012-05-08
    • 2010-12-03
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    相关资源
    最近更新 更多