简体   繁体   中英

How to remove ex color bar and create new one when reload data for subplot with heatmap seaborn in matplotlib?

I created a heatmap plot (using seaborn) in PyQT5 with first parameters. When changing parameters to create new heatmap plot in this canvas with new values but ex color bar not remove like this:

在此处输入图片说明

I indeed need only one color bar for plot that day (cbar=True). I don't want fix vmin, vmax for all plot with difference data values.

sns.heatmap(df_grid, annot=False, cmap='tab20', ax=self.axes_surface, cbar=True)

How to remove the ex color bar in this case?

You don't provide a lot of code. Here is a general way:

At the start of the program your create an ax for the main plot, and another one for the colorbar. width_ratios can help to have the colorbar occupy much less space than the main heatmap.

my_fig, (my_ax, my_cbar_ax) = plt.subplots(ncols=2, figsize=(8, 6),
                                           gridspec_kw={'width_ratios': [10, 1]})

Inside the update function you provide both axes to sns.heatmap . If you don't provide the cbar_ax , seaborn will create a new colorbar every time you call your update function.

sns.heatmap(...., ax=my_ax, cbar_ax=my_cbar_ax)

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