简体   繁体   中英

Remove Labels and Tick-marks from Colorbar in Matplotlib

I have a heatmap and I want to remove the labels and tickmarks on the colorbar. How can I do this with the heatmap I've made so far (code below)?

import seaborn as sns
import matplotlib.pyplot as plt

sns.heatmap(my_heatmap, cmap='RdYlGn', vmin=0, vmax=1)
plt.rcParams["figure.figsize"] = (8, 6)
plt.title('10 mm Precipitation Probabilities\nTopeka, Kansas', fontsize = 16)
plt.xlabel('X', fontsize = 14)
plt.ylabel('Y', fontsize = 14)
plt.text(6.7,.18, 'High Probability')
plt.show()

在此处输入图像描述

(I would rather it just say High Probability at the top of the bar as shown with the label I made).

You can access the colorbar via cbar = ax.collections[0].colorbar (see eg this post ).

Then, you can change the ticks ( cbar.set_ticks([]) ) or set new ticks and new tick labels:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = (8, 6)
my_heatmap = np.random.rand(10, 10)
ax = sns.heatmap(my_heatmap, cmap='RdYlGn', vmin=0, vmax=1)
# ax.get_figure().set_size_inches(8, 6)
ax.set_title('10 mm Precipitation Probabilities\nTopeka, Kansas', fontsize=16)
ax.set_xlabel('X', fontsize=14)
ax.set_ylabel('Y', fontsize=14)
cbar = ax.collections[0].colorbar
cbar.set_ticks([0, 0.5, 1])
cbar.set_ticklabels(['Low\nProbability', 'Average\nProbability', 'High\nProbability'])
# cbar.set_label('Probability')
plt.tight_layout()
plt.show()

示例图

Note that plt.rcParams["figure.figsize"] doesn't change the size of the figure, but sets the default size for the next figure that will be created. So you need to call it before sns.heatmap which creates a new figure if none was created before. To change the size of an already-created figure, use set_size_inches() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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