首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >matplotlib matshow xtick标签在顶部和底部

matplotlib matshow xtick标签在顶部和底部
EN

Stack Overflow用户
提问于 2019-03-22 06:01:48
回答 2查看 10K关注 0票数 4

我正在尝试可视化一个名称共现矩阵。这个版本运行正常:

代码语言:javascript
运行
复制
import pandas as pd
import numpy as np
import string
import matplotlib.pyplot as plt

n = 10
names = ['Long Name ' + suffix for suffix in string.ascii_uppercase[:n]]
df = pd.DataFrame(np.random.randint(0, 100, size=(n,n)),
                  columns=names, index=names)

fig = plt.figure()
ax = plt.gca()

im = ax.matshow(df, interpolation='none')
fig.colorbar(im)

ax.set_xticks(np.arange(n))
ax.set_xticklabels(names)
ax.set_yticks(np.arange(n))
ax.set_yticklabels(names)
ax.xaxis.set_ticks_position("bottom")
plt.setp(ax.get_xticklabels(), rotation=45,
         ha="right", rotation_mode="anchor")

for (i,j), z in np.ndenumerate(df):
  if z != 0:
    ax.text(j, i, str(z), ha="center", va="center")

ax.set_title("Name Co-Occurrences")
fig.tight_layout()
plt.show()

问题是,我的实际矩阵相当大,所以我想在顶部和底部显示名称。我尝试过使用twiny来实现这一点

代码语言:javascript
运行
复制
import pandas as pd
import numpy as np
import string
import matplotlib.pyplot as plt

n = 10
names = ['Long Name ' + suffix for suffix in string.ascii_uppercase[:n]]
df = pd.DataFrame(np.random.randint(0, 100, size=(n,n)),
                  columns=names, index=names)

fig = plt.figure()
botax = plt.gca()

im = botax.matshow(df, interpolation='none')
fig.colorbar(im)

topax = botax.twiny()
for ax, ha, pos in zip([topax, botax], ["left", "right"], ["top", "bottom"]):
  ax.set_xticks(np.arange(n))
  ax.set_xticklabels(names)
  ax.set_yticks(np.arange(n))
  ax.set_yticklabels(names)
  ax.xaxis.set_ticks_position(pos)
  plt.setp(ax.get_xticklabels(), rotation=45,
           ha=ha, va="center", rotation_mode="anchor")

for (i,j), z in np.ndenumerate(df):
  if z != 0:
    botax.text(j, i, str(z), ha="center", va="center")

botax.set_title("Name Co-Occurrences")
fig.tight_layout()
plt.show()

不幸的是,顶部标签没有正确对齐,我不知道原因:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-22 06:50:59

为了标记轴的底部和顶部,不需要双轴。这可能会让这一切变得更容易一些。相反,您可以只打开底部和顶部刻度和标签,然后分别旋转和对齐它们。

代码语言:javascript
运行
复制
import pandas as pd
import numpy as np
import string
import matplotlib.pyplot as plt

n = 10
names = ['Long Name ' + suffix for suffix in string.ascii_uppercase[:n]]
df = pd.DataFrame(np.random.randint(0, 100, size=(n,n)),
                  columns=names, index=names)

fig = plt.figure()
ax = plt.gca()

im = ax.matshow(df, interpolation='none')
fig.colorbar(im)

ax.set_xticks(np.arange(n))
ax.set_xticklabels(names)
ax.set_yticks(np.arange(n))
ax.set_yticklabels(names)

# Set ticks on both sides of axes on
ax.tick_params(axis="x", bottom=True, top=True, labelbottom=True, labeltop=True)
# Rotate and align bottom ticklabels
plt.setp([tick.label1 for tick in ax.xaxis.get_major_ticks()], rotation=45,
         ha="right", va="center", rotation_mode="anchor")
# Rotate and align top ticklabels
plt.setp([tick.label2 for tick in ax.xaxis.get_major_ticks()], rotation=45,
         ha="left", va="center",rotation_mode="anchor")

ax.set_title("Name Co-Occurrences", pad=55)
fig.tight_layout()
plt.show()

票数 11
EN

Stack Overflow用户

发布于 2019-03-22 06:05:46

您必须首先将上x轴的纵横比设置为与下x轴的纵横比相同。如何做到这一点已经回答了here。然后,您可以使用y=1.3将标题向上提升一点,这样它就不会与上面的x轴记号标签重叠。

代码语言:javascript
运行
复制
topax = botax.twiny()

aspect0 = botax.get_aspect()
if aspect0 == 'equal':
    aspect0 = 1.0
dy = np.abs(np.diff(botax.get_ylim()))
dx = np.abs(np.diff(botax.get_xlim()))

aspect = aspect0 / (float(dy) / dx)
topax.set_aspect(aspect)

for ax, ha, pos in zip([topax, botax], ["left", "right"], ["top", "bottom"]):
  ax.set_xticks(np.arange(n))
  .....
  .....

botax.set_title("Name Co-Occurrences", y=1.3)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55289921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档