matplotlib 层次轴标注

1cosmwyk  于 2023-06-23  发布在  其他
关注(0)|答案(2)|浏览(99)

假设我在看一个n × n的网格,在每个轴上我都有动物的标签。但我也有兴趣研究组、亚组等之间的关系动物的故事。例如,我可能有脊椎动物和无脊椎动物,在脊椎动物中,我可能有哺乳动物和爬行动物等等。(如果重要的话,我对相关矩阵特别感兴趣,实际上我正在通过seaborn使用热图...)
我想在matplotlib中绘制这个图,但是沿着轴有层次标签。所以用我上面的例子,我会有像狗,猫,马,蜥蜴,鳄鱼等标签。然后第一组狗到马将具有哺乳动物的标签,第二组蜥蜴、鳄鱼等。会有爬行动物,这两个加在一起会有脊椎动物的进一步标签……
我该怎么做?

d8tt03nd

d8tt03nd1#

不幸的是,我不知道如何禁用次要的滴答声:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from mpl_toolkits.axes_grid.parasite_axes import SubplotHost

fig1 = plt.figure()
ax1 = SubplotHost(fig1, 111)
fig1.add_subplot(ax1)

# Some data
x = np.arange(1,6)
y = np.random.random(len(x))

# First X-axis
ax1.plot(x, y)
ax1.set_xticks(x)
ax1.set_xticklabels(['dog', 'cat', 'horse', 'lizard', 'crocodile'])
#ax1.xaxis.set_label_text('First X-axis') # Uncomment to label axis
ax1.yaxis.set_label_text("Sample data")

# Second X-axis
ax2 = ax1.twiny()
offset = 0, -25 # Position of the second axis
new_axisline = ax2.get_grid_helper().new_fixed_axis
ax2.axis["bottom"] = new_axisline(loc="bottom", axes=ax2, offset=offset)
ax2.axis["top"].set_visible(False)

ax2.set_xticks([0.0, 0.6, 1.0])
ax2.xaxis.set_major_formatter(ticker.NullFormatter())
ax2.xaxis.set_minor_locator(ticker.FixedLocator([0.3, 0.8]))
ax2.xaxis.set_minor_formatter(ticker.FixedFormatter(['mammal', 'reptiles']))

# Third X-axis
ax3 = ax1.twiny()
offset = 0, -50
new_axisline = ax3.get_grid_helper().new_fixed_axis
ax3.axis["bottom"] = new_axisline(loc="bottom", axes=ax3, offset=offset)
ax3.axis["top"].set_visible(False)

ax3.set_xticks([0.0, 1.0])
ax3.xaxis.set_major_formatter(ticker.NullFormatter())
ax3.xaxis.set_minor_locator(ticker.FixedLocator([0.5]))
ax3.xaxis.set_minor_formatter(ticker.FixedFormatter(['vertebrates']))

ax1.grid(1)
plt.show()

编辑

1.通过将ticksize设置为0可以禁用次要tick(感谢@arnsholt):ax2.axis["bottom"].minor_ticks.set_ticksize(0)
1.在最新的matplotlib版本(3.0.0或更高版本)中,SubplotHost必须导入为:
from mpl_toolkits.axisartist.parasite_axes import SubplotHost

km0tfn4u

km0tfn4u2#

这里是@Vadim的答案的一个稍微更新的版本,因为我找到了一种不使用SubplotHost的方法。这样,即使您没有创建子图,也可以执行此操作(例如,在使用seaborn图形级函数时)。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

fig1, ax1 = plt.subplots(1)
# Some data
x = np.arange(1,6)
y = np.random.random(len(x))

# First X-axis
ax1.plot(x, y)
ax1.set_xticks(x)
ax1.set_xticklabels(['dog', 'cat', 'horse', 'lizard', 'crocodile'])
ax1.yaxis.set_label_text("Sample data")

# Second X-axis
ax2 = ax1.twiny()

ax2.spines["bottom"].set_position(("axes", -0.10))
ax2.tick_params('both', length=0, width=0, which='minor')
ax2.tick_params('both', direction='in', which='major')
ax2.xaxis.set_ticks_position("bottom")
ax2.xaxis.set_label_position("bottom")

ax2.set_xticks([0.0, 0.6, 1.0])
ax2.xaxis.set_major_formatter(ticker.NullFormatter())
ax2.xaxis.set_minor_locator(ticker.FixedLocator([0.3, 0.8]))
ax2.xaxis.set_minor_formatter(ticker.FixedFormatter(['mammal', 'reptiles']))

# Third X-axis
ax3 = ax1.twiny()

ax3.spines["bottom"].set_position(("axes", -0.20))
ax3.tick_params('both', length=0, width=0, which='minor')
ax3.tick_params('both', direction='in', which='major')
ax3.xaxis.set_ticks_position("bottom")
ax3.xaxis.set_label_position("bottom")

ax3.set_xticks([0.0, 1.0])
ax3.xaxis.set_major_formatter(ticker.NullFormatter())
ax3.xaxis.set_minor_locator(ticker.FixedLocator([0.5]))
ax3.xaxis.set_minor_formatter(ticker.FixedFormatter(['vertebrates']))

ax1.grid(True)
plt.show()

相关问题