gpt4 book ai didi

python - matplotlib matshow xtick 标签在顶部和底部

转载 作者:行者123 更新时间:2023-11-28 20:31:18 24 4
gpt4 key购买 nike

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

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 来做到这一点:

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

不幸的是顶部标签没有正确对齐,我不知道为什么:Misaligned X tick labels

最佳答案

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

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

enter image description here

关于python - matplotlib matshow xtick 标签在顶部和底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55289921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com