How to solve the issue associated with MatplotlibDeprecationWarning?

Hi everyone,

How can I provide either the cax argument to use as the Axes for the Colorbar, or provide the ax argument to steal space from it, or add mappable to an Axes. Although I can plot my chart successfully, I can’t avoid the warning. Any help you render would be much appreciated. Thank you very much.

My codes:

# plot chart based on predicted value
fig, ax = plt.subplots()
colorsMap = 'jet'
cm = plt.get_cmap(colorsMap)
cNorm = matplotlib.colors.Normalize(vmin=min(yhat), vmax=max(yhat))
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)
ax.scatter(Y, yhat, c=scalarMap.to_rgba(yhat), s=2, alpha=0.7)
scalarMap.set_array(yhat)
fig.colorbar(scalarMap)
ax.set_xlabel('Actual')
ax.set_ylabel('Predicted')
ax.text(0.85, 0.5, 'R^2=0.76', horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
# add diagonal line
ident = [np.min(Y), np.max(Y)]
plt.plot(ident, ident, color='red')
# save yhat plot
plt.savefig('Predicted.png', dpi=600)

I think you just need to pass ax=ax in your call to colorbar. That tells it that you want to make your main axes a little smaller to make space for the color bar.

Dear rcomer,

Thanks for your help. I fixed this error by trials and errors. My codes are as follows:

plot chart based on predicted value (Test)

fig, ax = plt.subplots()
cNorm = matplotlib.colors.Normalize(vmin=min(yhat2), vmax=max(yhat2))
scalarMap = cmx.ScalarMappable(norm=cNorm)
scatterplot = ax.scatter(Y_test, yhat2, c=yhat2, cmap=‘jet’, s=2, alpha=0.7)
fig.colorbar(scatterplot, ax=ax)
ax.set_xlabel(‘Actual’)
ax.set_ylabel(‘Predicted’)
ax.text(0.85, 0.5, ‘R^2=0.72’, horizontalalignment=‘center’, verticalalignment=‘center’, transform=ax.transAxes)

add diagonal line

ident = [np.min(Y_test), np.max(Y_test)]
plt.plot(ident, ident, color=‘red’)

save yhat plot

plt.savefig(‘Predicted2.png’, dpi=600)

Regards,

pallidness

Ruth Comer via Matplotlib <nobody@discourse.matplotlib.org> 於 2023年10月12日 週四 上午3:58寫道:

···

| rcomer
October 11 |

  • | - |

I think you just need to pass ax=ax in your call to colorbar. That tells it that you want to make your main axes a little smaller to make space for the color bar.


Visit Topic or reply to this email to respond.

To unsubscribe from these emails, click here.

1 Like

Dear rcomer,

Thanks for your help. I fixed this error by trials and errors. My codes are as follows:

# plot chart based on predicted value (Test)
fig, ax = plt.subplots()
cNorm = matplotlib.colors.Normalize(vmin=min(yhat2), vmax=max(yhat2))
scalarMap = cmx.ScalarMappable(norm=cNorm)
scatterplot = ax.scatter(Y_test, yhat2, c=yhat2, cmap='jet', s=2, alpha=0.7)
fig.colorbar(scatterplot, ax=ax)
ax.set_xlabel('Actual')
ax.set_ylabel('Predicted')
ax.text(0.85, 0.5, 'R^2=0.72', horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
# add diagonal line
ident = [np.min(Y_test), np.max(Y_test)]
plt.plot(ident, ident, color='red')
# save yhat plot
plt.savefig('Predicted2.png', dpi=600)

Regards,

pallidness