Rififi - 4 months ago 14
Python Question

# Axis don't show the ticks I want

I want to plot a Ramachandron plot. On this kind of graph, x goes from -180° to 180°, and so does y. I want a tick every 60 degrees. So here is the code I use:

``````import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

x = [-179, 179]
y = [-179, 179]

fig = plt.figure(1)
ax = plt.subplot(111)

ax.axis([-180, 180, -180, 180])

ax.set_xticks([-180, -120, -60, 0, 60, 120, 180])
ax.set_yticks([-180, -120, -60, 0, 60, 120, 180])

# 1 bim = 1 degree
# !!! Logarithmic normalization of the colors
plt.hist2d(x, y, bins=180, norm=LogNorm())
plt.colorbar()
plt.show()
``````

On this working example, I'm plotting only two points. But the ticks -180 and 180 are not shown, for none of the axes:

If I change x and y to:

``````x = [-180, 180]
y = [-180, 180]
``````

I get what I want:

Is there a way to achieve the second result without altering the data ?

Answer

You use hist2d, set axis ticks after plotting:

``````import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

x = [-179, 179]
y = [-179, 179]

fig = plt.figure(1)
ax = plt.subplot(111)

# 1 bim = 1 degree
# !!! Logarithmic normalization of the colors
plt.hist2d(x, y, bins=180, norm=LogNorm())
plt.colorbar()

ax.axis([-180, 180, -180, 180])
ax.set_xticks([-180, -120, -60, 0, 60, 120, 180])
ax.set_yticks([-180, -120, -60, 0, 60, 120, 180])

plt.show()
``````

Source (Stackoverflow)
Comments