0

I am making a scatter plot in matplotlib with logarithmic color scaling, which is working fine, see attached plot. My problem is, I would like to have the x-tick labels on the r.h.s. of the color bar to be in float format, rather than scientific notation. Interestingly, this works only for some of the labels.

I have my data x, y for the scatter plot and the weights which specify the colors. This is my code:

fig = plt.figure(dpi=200)

# Plot data, this is the relevant part:
sc = plt.scatter(x, y, c=weights, cmap='rainbow', s=20, alpha=0.5,
                 norm=mpl.colors.LogNorm(vmin=0.3, vmax=3))

cbar = fig.colorbar(sc, format='%.1f', label='$T_{IDL} / T_{Py}$')  # format arg. supposed to do what I want
print(cbar.get_ticks()) # For debugging

# Plot dashed line:
xmin, xmax = plt.gca().get_xlim()
const = np.linspace(xmin, xmax, 500)
plt.plot(const, const, linestyle='--', color='black', alpha=0.3)

# Add titles and axis labels:
fig.suptitle(suptitle)
plt.title(f'{len(amp_py_cmn)} Common Flares -- Duration Ratio: Mean {np.mean(dur_ratio):.2f},'
          f' St.Dev. {np.std(dur_ratio):.2f}',
          fontsize=7)

plt.xlabel('$A_{Py}$')
plt.ylabel('$A_{IDL}$')

# Save figure and show:
fig.savefig(f'{savePath}/{suptitle}.pdf')
plt.show()

This is the resulting Plot:

Scatter Plot

I added the call of cbar.get_ticks() while debugging, and interestingly the output gives

[1.]

which corresponds to the only label that looks according to my wishes. So the question is, where do the other labels come from, and how can I format them? Thanks!

  • 1
    These are the minor ticks. You probably want to do something like `cbar.ax.yaxis.set_minor_formatter(FormatStrFormatter("%.2f"))` – Jody Klymak Jul 04 '22 at 18:45
  • See also https://stackoverflow.com/questions/25983218/scientific-notation-colorbar-in-matplotlib – user_na Jul 04 '22 at 18:52
  • @user_na Thanks for the comment. As far as I see, that question deals with formatting of the tick labels in general, but does not address the actual issue that I had, which was accessing the minor ticks specifically. – Tobi_VM Jul 05 '22 at 19:05

0 Answers0