Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading