Matplotlib对箱线图的不同侧面进行滴答

时间:2016-03-16 23:07:28

标签: python matplotlib

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

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

但我需要:

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

1 个答案:

答案 0 :(得分: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()

enter image description here