can you possibly generate a color palette for every color value (from -20 to 40C), base…
- User
can you possibly generate a color palette for every color value (from -20 to 40C), base it on the current values that are provided each 5 values. (so it should be looking way more high resolution, but keep the color palette comfortable and aesthetic for eyes as looking a temperature plot). As the output, send only an updated color table: import xarray as xr import matplotlib.pyplot as plt import matplotlib.colors as mcolors import numpy as np
ds = xr.open_dataset('ALADIN.grb', engine='cfgrib')
variable = 't2m'
Convert temperature from Kelvin to Celsius ds[variable] = ds[variable] - 273.15
custom_colors = [ (38,67,109,255), (35,80,122,255), (39,91,128,255), (39,103,138,255), (40,117,147,255), (64,130,144,255), (97,142,137,255), (135,154,134,255), (171,168,125,255), (193,172,117,255), (193,157,97,255), (195,138,83,255), (190,112,74,255), (175,77,78,255), (157,42,75,255), (135,32,62,255), (110,21,49,255), (85,13,37,255), (64,1,20,255) ]
rgb_colors = [(r/255, g/255, b/255) for r, g, b, _ in custom_colors]
custom_cmap = mcolors.LinearSegmentedColormap.from_list("custom", rgb_colors)
Create two separate figures: one for saving and one for display fig_save = plt.figure(frameon=False) ax_save = plt.Axes(fig_save, [0., 0., 1., 1.]) ax_save.set_axis_off() fig_save.add_axes(ax_save)
fig_display = plt.figure(figsize=(10, 8)) ax_display = fig_display.add_subplot(111)
Set the temperature range vmin, vmax = -20, 40
Plot for saving im_save = ax_save.imshow(ds[variable].isel(step=70), cmap=custom_cmap, vmin=vmin, vmax=vmax)
Plot for display im_display = ax_display.imshow(ds[variable].isel(step=70), cmap=custom_cmap, vmin=vmin, vmax=vmax)
Add colorbar and title to the display plot cbar = fig_display.colorbar(im_display, ax=ax_display, extend='both') cbar.set_label('Temperature (°C)') ax_display.set_title(f'2m Temperature at step 70')
Save the figure without background plt.figure(fig_save.number) plt.savefig(f'{variable}_celsius.png', dpi=300, bbox_inches='tight', pad_inches=0) print(f"Custom colormap plot saved as {variable}_celsius.png")
Show the display figure with background, colorbar, and title plt.figure(fig_display.number) plt.show()
Close both figures plt.close(fig_save) plt.close(fig_display)
Assistant
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.