Exemple de comment sauvegarder uniquement l'échelle de couleur d'une figure avec matplotlib (d'apres Andras Deak: Save colorbar for scatter plot separately)
import matplotlib.pyplot as plt
import numpy as np
def f(x,y):
return (x+y)*np.exp(-5.0*(x**2+y**2))
X,Y = np.mgrid[-1:1:100j, -1:1:100j]
Z = f(X,Y)
plt.figure()
mpb = plt.pcolormesh(X,Y,Z,cmap='viridis')
plt.title('How to save the colorbar separartly in matplotlib ?',fontsize=10)
# plot the original without a colorbar
plt.savefig('plot_nocbar.png')
# plot a colorbar into the original to see distortion
plt.colorbar()
plt.savefig('plot_withcbar.png')
# draw a new figure and replot the colorbar there
fig,ax = plt.subplots()
plt.colorbar(mpb,ax=ax)
ax.remove()
plt.savefig('plot_onlycbar.png')
# save the same figure with some approximate autocropping
plt.savefig('plot_onlycbar_tight.png',bbox_inches='tight')
Références
Liens | Site |
---|---|
Save colorbar for scatter plot separately | stackoverflow |
Axes.remove() | matplotlib.org |
matplotlib.pyplot.colorbar | matplotlib.org |
matplotlib.pyplot.savefig | matplotlib.org |