jackaraz
jackaraz

Reputation: 291

modifying tick numbering in colorbar

Hi I'm trying to change the numbering on colorbar my code gives me an unexpected number on colorbar (+1.552, numbers are correct I just dont understand the format) . My graph is as follows:

enter image description here

I want to format those ticks on color bar as 1.5, 1.52,1.54 etc. The code that Im using is as follows:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.unicode'] = True

with open("psi_newdata.dat") as f:
    data = f.read()

data = data.split('\n')
cm = plt.cm.get_cmap('RdYlBu')
x = [float(row.split('\t')[12]) for row in data]
y = [float(row.split('\t')[14]) for row in data]
z = [float(row.split('\t')[8])/1000.0 for row in data]

cm = plt.cm.get_cmap('YlGnBu')
fig, ax = plt.subplots()
ax.set_yticks([0.1224,0.1172], minor=True)
ax.yaxis.grid(True, which='minor')

sc = ax.scatter(x, y, c=z,cmap=cm)
cbar = plt.colorbar(sc)
plt.xlabel(r'$\displaystyle M_{\tilde{\nu}_R}$\ [GeV]')
plt.ylabel(r'$\displaystyle\Omega h^2$')
cbar.set_label(r"$\displaystyle M_{Z^{'}}$\ [TeV]")
plt.show()

Upvotes: 0

Views: 2266

Answers (1)

Oliver W.
Oliver W.

Reputation: 13459

The format you're seeing on the colorbar is a shorthand notation. If you'd like to see the classical format without the +1.552 offset, just specify the desired format in the call to colorbar:

import numpy as np
import matplotlib.pyplot as plt

# attempt to duplicate your data
x = np.arange(10)
y = x
z = 1.552 + np.linspace(0, 0.0030,10)

plt.scatter(x,y,c=z)
cbar = plt.colorbar(format="%.4f")

The colorbar method also allows you to specify where exactly you want the ticks:

cbar = plt.colorbar(ticks=[1.5523, 1.5547], format="%.4f")

Note that you should specify both the format as well as the ticks, otherwise the specified ticks will still use the offset on the top of the colorbar.

The figure below demonstrates this: example of preventing the offset from appearing on a colorbar

Upvotes: 4

Related Questions