1

How can I re-format the tick labels on my colorbar such that they are not in standard form. I would like the labels to read 0.02, 0.03, 0.04 ..., 0.2, NOT 2 x 10^-2, 3 x 10^-2 etc, whilst retaining the logarithmic scale. See image below for the current image.

#Gf
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp2d
from mpl_toolkits.axes_grid1 import make_axes_locatable
from astropy.visualization import simple_norm
from matplotlib.colors import LogNorm
from matplotlib.ticker import LogFormatter 

x_list = np.array([x * 3 for x in X_COORDINATE])
z_list = np.array(Z_COORDINATE)
Gf_list = np.array(Gf)

x = np.arange(5.3, 30, 1.5)
z = np.arange(0.0, 12.0, 1.5)
#X, Z = np.meshgrid(x, z)

#print (X.shape)

f = interp2d(x_list,z_list,Gf_list,kind='linear')
gf_i = f(x,z)

fig = plt.imshow(gf_i,
           extent=[min(x_list),max(x_list),min(z_list),max(z_list)],
           origin="lower", interpolation='bicubic', norm=LogNorm(), cmap='YlGn', vmin=0.02, vmax=0.2)

# Show the positions of the sample points, just to have some reference
fig.axes.set_autoscale_on(False)
#plt.plot(x_for_horizontal_line, y_for_horizontal_line, color='w')
#plt.plot(x_for_vertical_line, y_for_vertical_line, color='w')
plt.xlim(left=3,right=15)
plt.ylim(top=8, bottom=0)
#plt.ylim(top=,bottom=)
plt.xlabel('NF Center, x (m)')
plt.ylabel('NF Center, z (m)')
plt.title('$G_{f}$ - 0.002 Pa s')
plt.scatter(x_list,z_list,20,color='k',facecolors='none', marker='.')
#plt.scatter(x_list,z_list,400,color='k',facecolors='none')
ax = plt.gca()
ax.set_xticks([3,6,9,12,15]) # choose which x locations to have ticks
ax.set_xticklabels([1,2,3,4,5]) 
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.1)
plt.rcParams.update({'font.size':14})
formatter = LogFormatter(10, labelOnlyBase=False) 
plt.colorbar(fig, cax=cax)
plt.savefig('Gf_final_0.002PaS.svg',bbox_inches='tight', dpi=1200)
plt.show()

enter image description here

martineau
  • 119,623
  • 25
  • 170
  • 301
chelseabej1998
  • 111
  • 1
  • 6
  • 1
    Does this answer your question? [Matplotlib log scale tick label number formatting](https://stackoverflow.com/questions/21920233/matplotlib-log-scale-tick-label-number-formatting) – JohanC Jul 25 '20 at 09:45
  • No, I need to change the colorbar ticks not my plot axes.. – chelseabej1998 Jul 25 '20 at 10:22
  • 1
    Well, they are the same type of axis. `cbar = plt.colorbar(...)` and then `cbar.ax.yaxis.set_major_formatter(...)` – JohanC Jul 25 '20 at 10:50
  • See also [How to set colorbar for line plot with log scale](https://stackoverflow.com/questions/54513247/matplotilb-how-to-set-colorbar-for-line-plot-with-log-scale) – JohanC Jul 25 '20 at 10:59

0 Answers0