简体   繁体   English

在 matplotlib seaborn 中使用 add_gridspec 时如何删除刻度线和标签

[英]How to remove ticks lines and labels when using add_gridspec in matplotlib seaborn

This is a follow on from my Previous Question这是我上一个问题的后续

I have the following code:我有以下代码:

#Plot

cmap = plt.cm.GnBu_r
f, ax = plt.subplots(nrows=4,figsize=(20,10))
spec = f.add_gridspec(ncols = 1, nrows = 4, height_ratios = [1,1,1,.3])


ax0 = f.add_subplot(spec[0])
ax0 = sns.heatmap(df1,cbar=False,cmap=cmap)

ax1 = f.add_subplot(spec[1])
ax1 = sns.heatmap(df2,cbar=False,cmap=cmap)

ax2 = f.add_subplot(spec[2])
ax2 = sns.heatmap(df3,cbar=False,cmap=cmap)

ax3 = f.add_subplot(spec[3])
ax3 = sns.heatmap(df4,cbar=False,cmap=cmap)


axis = [ax0,ax1,ax2,ax3]
names = ['1', '2', '3','4']

for axi in axis:
    n = axis.index(axi)
    axi.set_title(str(names[n]))
    axi.set(xticklabels=[])
    axi.set_xlabel('')
    axi.xaxis.set_visible(False)
    h = axi.get_yticks()
    w = axi.get_xticks()
    axi.vlines(25, h[0] - 0.5, h[-1] + .5, linewidth=1, color="black")
    axi.vlines(50,h[0]-0.5,h[-1]+.5, linewidth=1, color="black")
    axi.vlines(75,h[0]-0.5,h[-1]+.5, linewidth=1, color="black")
    axi.vlines(100,h[0]-0.5,h[-1]+.5, linewidth=2, color="black")
    axi.set_ylabel('')

Which gives me the following output:这给了我以下 output:

在此处输入图像描述

I have blacked out the names that I want to show but I am wondering how to hide the other x and y labels?我已将要显示的名称涂黑,但我想知道如何隐藏其他 x 和 y 标签? eg all the decimals.例如所有小数。 I think they are related to the gridspec boxes for the 4 indivdual plots but i dont know how to remove them.我认为它们与 4 个单独地块的 gridspec 框有关,但我不知道如何删除它们。 I can remove the xticklabels and xlabel but it doesnt seem to work on the decimal points.我可以删除xticklabelsxlabel但它似乎不适用于小数点。

Summary Question: How do I remove the decimal points in the plots?摘要问题:如何删除图中的小数点? Any help would be much appreciated!任何帮助将非常感激! Thanks!谢谢!

If I understood well, you can remove specific ticks as decribed here .如果我理解得很好,您可以删除此处描述的特定刻度。 By using通过使用

yticks = axi.yaxis.get_major_ticks()
yticks[k].set_visible(False)

EDITED已编辑

Here is an example这是一个例子

import matplotlib
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
y_dot = [0,1,2,3,4,5,6,7]
x_dot = [0,1,2,3,4,5,6,7]

xticks = ax.xaxis.get_major_ticks()

for x in x_dot:
    for y in y_dot:
        ax.plot(x, y, 'ro')
        
l_x = [1,3,5]
for element in l_x:
    xticks[element].set_visible(False)

yticks = ax.yaxis.get_major_ticks()
l_y = [2,4,6]
for element in l_y:
    yticks[element].set_visible(False)

With the following output:使用以下 output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM