matplotlib (Python) 中的层次轴标记 [英] Hierarchical axis labeling in matplotlib (Python)

查看:25
本文介绍了matplotlib (Python) 中的层次轴标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在看一个n x n的网格,并且在每个轴上我都有动物的标签.但是我也对研究动物的组,子组等之间的关系感兴趣.例如,我可能有脊椎动物和无脊椎动物,在脊椎动物中我可能有哺乳动物和爬行动物等等.(如果重要的话,我对相关矩阵特别感兴趣,实际上我正在通过 seaborn 使用热图......)

我想在matplotlib中进行绘制,但沿轴具有分层标签.所以使用我上面的例子,我会有像狗、猫、马、蜥蜴、鳄鱼等的标签,然后第一组狗到马会有哺乳动物的标签,第二组蜥蜴、鳄鱼等会有标签有爬行动物,而这两个一起将有脊椎动物的进一步标签...

我该怎么做?

解决方案

不幸的是,我不知道如何禁用小刻度线:

将 numpy 导入为 np导入matplotlib.pyplot作为plt导入matplotlib.ticker作为股票行情从mpl_toolkits.axes_grid.parasite_axes导入SubplotHost图1 = plt.figure()ax1 = SubplotHost(fig1,111)fig1.add_subplot(ax1)# 一些数据x = np.arange(1,6)y = np.random.random(len(x))# 第一个 X 轴ax1.plot(x, y)ax1.set_xticks(x)ax1.set_xticklabels(['狗', '猫', '马', '蜥蜴', '鳄鱼'])#ax1.xaxis.set_label_text('First X-axis') # 取消对标签轴的注释ax1.yaxis.set_label_text(样本数据")# 第二个 X 轴ax2 = ax1.twiny()offset = 0,-25#第二轴的位置new_axisline = ax2.get_grid_helper().new_fixed_axisax2.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(['哺乳动物','爬行动物']))#第三X轴ax3 = ax1.twiny()偏移= 0,-50new_axisline = ax3.get_grid_helper().new_fixed_axisax3.axis ["bottom"] = new_axisline(loc ="bottom",axis = 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(['脊椎动物']))ax1.grid(1)plt.show()

<小时>

编辑:

  1. 可以通过将ticksize设置为0(感谢@arnsholt)来禁用次要滴答: ax2.axis ["bottom"].minor_ticks.set_ticksize(0).

  2. >
  3. 在最新的 matplotlib 版本(3.0.0 或更高版本)SubplotHost 必须导入为:

    from mpl_toolkits.axisartist.parasite_axes import SubplotHost

Suppose I'm looking at an n x n grid and on each axis I have labels of say, animals. But I'm also interested at looking at the relationship between groups, subgroups, etc. of the animals. So for example, I may have vertebrates and invertebrates, within vertebrates I may have mammals and reptiles and so forth. (If it matters, I'm particularly interested in a correlation matrix and am actually using a heatmap via seaborn...)

I'd like to plot this in matplotlib but have hierarchical labeling along the axes. So using my above example, I would have labels like dog, cat, horse, lizard, crocodile, etc. and then the first group of dog through horse would have a label of mammal and the second group of lizard, crocodile, etc. would have reptiles, and those two together would have a further label of vertebrates...

How would I do this?

解决方案

Unfortunately I can't figure out how to disable minor ticks:

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


EDIT:

  1. Disabling minor ticks could be done by setting ticksize to 0 (thanks to @arnsholt): ax2.axis["bottom"].minor_ticks.set_ticksize(0).

  2. In latest matplotlib version (3.0.0 or higher) SubplotHost has to be imported as:

    from mpl_toolkits.axisartist.parasite_axes import SubplotHost

这篇关于matplotlib (Python) 中的层次轴标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆