user3076813
user3076813

Reputation: 519

How to make matplotlib/pcolor show the last row and column of data

matplotlib/pcolor chops off the last row and column of data. This post suggests that increasing the dimensions of x and y arguments by one should resolve the problem. Now consider the following simple example:

x = np.array([1,2])
y = np.array([1,2])
z = np.array([[10,20],[30,40]])

# Using pcolor
fig, ax = plt.subplots()
p = ax.pcolor(x,y,z,vmin=z.min(), vmax=z.max())
ax.set_xlim([x.min(),x.max()])
ax.set_ylim([y.min(),y.max()])
cb = fig.colorbar(p, ax=ax)

# Using matshow (for comparison)
fig, ax = plt.subplots()
ms = ax.matshow(z,vmin=z.min(), vmax=z.max(), origin = 'lower')
ax.xaxis.tick_bottom()
cb = fig.colorbar(ms, ax=ax)

Increasing the dimensions of x and y by one e.g., by changing them to:

x = np.array([1,2,2.1])
y = np.array([1,2,2.1])

does not solve the issue. How can I make the pcolor plot to look similar to matshow plot?

Upvotes: 1

Views: 1344

Answers (1)

Francesc Torradeflot
Francesc Torradeflot

Reputation: 126

Try this:

import numpy as np
from matplotlib import pyplot as plt

x = np.array([1,2,3])
y = np.array([1,2,3])
z = np.array([[10,20],[30,40]])

# Using pcolor
fig, ax = plt.subplots()
p = ax.pcolor(x,y,z,vmin=z.min(), vmax=z.max())
ax.set_xlim([x.min(),x.max()])
ax.set_ylim([y.min(),y.max()])
cb = fig.colorbar(p, ax=ax)
plt.show()

Edit:

x = np.array([0.5,1.5,2.5])
y = np.array([0.5,1.5,2.5])
z = np.array([[10,20],[30,40]])

# Using pcolor
fig, ax = plt.subplots()
p = ax.pcolor(x,y,z,vmin=z.min(), vmax=z.max())
ax.set_xticks([1., 2.])
ax.set_yticks([1., 2.])
cb = fig.colorbar(p, ax=ax)
plt.show()

Upvotes: 1

Related Questions