Cupitor
Cupitor

Reputation: 11657

Incomplete ticks in matplotlib

I am changing the ticks for an imshow plot manually but the new ticks is incomplete and does not go through all the range that it has. What is the reason?

Here is a simple version of my code:

from matplotlib import pyplot as plt
import numpy as np
a = np.random.randn(30,50)
fig, ax = plt.subplots(1, 1)
x_range = np.around((np.arange(50)/10.) / np.sqrt(5.5), decimals=2)
y_range = np.around((np.arange(30)/10.) / np.sqrt(2.5), decimals=2)
plt.imshow(a)
ax.set_xticklabels(x_range)
ax.set_yticklabels(y_range)
plt.show()

and here is the result: enter image description here

Upvotes: 0

Views: 294

Answers (2)

JD Forster
JD Forster

Reputation: 175

Please be more precise in your question and provide a code that fully describes your problem. Of course you can address single plots, but you need to pass plot indices then!

    fig, ax = plt.subplots(nrows=1, ncols=2)
    x_range = np.around((np.arange(50)/15.) / np.sqrt(5.5), decimals=2)
    y_range = np.around((np.arange(30)/15.) / np.sqrt(2.5), decimals=2)
    x_range2 = np.around((np.arange(5)/15.) / np.sqrt(5.5), decimals=2)
    y_range2 = np.around((np.arange(3)/15.) / np.sqrt(5.5), decimals=2)
    ax[0].imshow(a,extent=[min(x_range),max(x_range),min(y_range),max(y_range)])
    ax[1].imshow(a,extent=[min(x_range2),max(x_range2),min(y_range2),max(y_range2)])
    ax[0].set_xticks(x_range)
    ax[1].set_xticks(x_range2)
    ax[0].set_yticks(y_range)
    ax[1].set_yticks(y_range2)

Also be aware of that set_xticklabels() only changes the actual naming of the label, where set_xticks() also refers to the real position in your plot.

Upvotes: 1

JD Forster
JD Forster

Reputation: 175

You want to replace

plt.imshow(a)
ax.set_xticklabels(x_range)
ax.set_yticklabels(y_range)

by

plt.imshow(a,extent=[min(x_range),max(x_range),min(y_range),max(y_range)])
plt.xticks(x_range)
plt.yticks(y_range)

Upvotes: 1

Related Questions