Nicholas Callahan
Nicholas Callahan

Reputation: 153

Make axes unequal in matplotlib or seaborn

In MATLAB, using surf a colleague was able to make the heatmap/surface below. I have been trying to generate plots using the same data in both matplotlib and seaborn, but cannot seem to find any way to make the x-axis unequally spaces like MATLAB does.

Is there a way out of the box to make sure my x-axis does not become equally spaced?

enter image description here

Upvotes: 0

Views: 762

Answers (1)

JohanC
JohanC

Reputation: 80509

Seaborn's heatmap (based on matplotlib's imshow) always creates categorical axes. You can use matplotlib's pcolor or pcolormesh to set numeric x and y values for the cell edges.

Here is an example. Note that the number of data values is one less in both directions compared to the number of edges.

import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
import numpy as np

np.random.seed(111)
xs = [-5, -3, -2, -1, -0.5, 0, 0.5, 1, 2, 3, 5]
ys = np.arange(1500, 5001, 500)
data = gaussian_filter(np.random.rand(len(ys) - 1, len(xs) - 1), 2)

fig, ax = plt.subplots()
ax.pcolor(xs, ys, data, cmap='inferno')
ax.set_xticks(xs)
ax.set_yticks(ys)
ax.grid(True, axis='both', color='skyblue')
plt.show()

plt.pcolor with uneven cell borders

Upvotes: 2

Related Questions