[英]Matplotlib: How to hide/remove ticks of Colorbar when it has no actual labels?
[英]How to remove ticks lines and labels when using add_gridspec in matplotlib seaborn
這是我上一個問題的后續
我有以下代碼:
#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('')
這給了我以下 output:
我已將要顯示的名稱塗黑,但我想知道如何隱藏其他 x 和 y 標簽? 例如所有小數。 我認為它們與 4 個單獨地塊的 gridspec 框有關,但我不知道如何刪除它們。 我可以刪除xticklabels
和xlabel
但它似乎不適用於小數點。
摘要問題:如何刪除圖中的小數點? 任何幫助將非常感激! 謝謝!
如果我理解得很好,您可以刪除此處描述的特定刻度。 通過使用
yticks = axi.yaxis.get_major_ticks()
yticks[k].set_visible(False)
已編輯
這是一個例子
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)
使用以下 output:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.