Matplotlib.pyplot.imshow with uneven x axis

Hello,

I am struggling with matplotlib.pyplot.imshow to display the slope percentages on an elevation graph. Slope values are calculated on a uneven x axis. However, there are displayed as if they were equally spaced:

Extract of the DataFrame used:

      distance  latitude  longitude  elevation     slope
0     0.000000  45.14746    5.75135      246.0  0.000000
1     0.129540  45.14728    5.74972      244.8 -0.926358
2     0.169508  45.14710    5.74928      243.2 -4.003190
3     0.248422  45.14731    5.74832      242.8 -0.506876
4     0.276006  45.14731    5.74797      240.4 -8.700743
..         ...       ...        ...        ...       ...
485  78.933911  45.14713    5.74540      231.8  1.913190
486  79.124953  45.14792    5.74756      233.8  1.046895
487  79.212118  45.14869    5.74776      235.8  2.294491
488  79.438727  45.14991    5.75007      240.0  1.853416
489  79.733123  45.14743    5.75137      246.0  2.038068

Extract of code used to render the plot:

distances = PyGraph.pointsDf["distance"]
elevations = PyGraph.pointsDf["elevation"]
slopes = PyGraph.pointsDf["slope"]

path = Path(
    np.array([np.append(distances, distances[::-1]), np.append(elevations, np.zeros_like(elevations))]).T
)
patch = PathPatch(path, facecolor="none")
self._axes.add_patch(patch)

im = self._axes.imshow(
    slopes.to_numpy().reshape(1, elevations.size),
    cmap=plt.cm.jet,
    interpolation="none",
    extent=[0, np.max(distances), np.min(elevations), np.max(elevations)],
    aspect="auto",
    clip_path=patch,
    clip_on=True,
)

divider = make_axes_locatable(self._axes)
cax = divider.append_axes("right", size="1%", pad=0.1)
self._figure.colorbar(im, cax=cax, label="Slope (%)")

Is it possible to use imshow with an uneven x axis ? If not, how can I change my DataFrame to have slopes calculated based on a constant distance interval ?

Source: python - How to fill a line graph with a color that changes by slope? - Stack Overflow

Hi @vcottineau you may find pcolormesh better than imshow for your use-case.
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.pcolormesh.html#matplotlib.axes.Axes.pcolormesh

1 Like

Hi @rcomer, thanks for the advice. It’s looking way better now:

Extract of code used to render the plot:

distances = PyGraph.pointsDf["distance"]
elevations = PyGraph.pointsDf["elevation"]
slopes = PyGraph.pointsDf["slope"].tail(-1)

path = Path(
    np.array([np.append(distances, distances[::-1]), np.append(elevations, np.zeros_like(elevations))]).T
)
patch = PathPatch(path, facecolor="none")
self._axes.add_patch(patch)

pcolormesh = self._axes.pcolormesh(
    distances,
    [elevations.min(), elevations.max()],
    np.reshape(slopes, (1, len(distances) - 1)),
    cmap=plt.cm.jet,
    clip_path=patch,
    clip_on=True
)

divider = make_axes_locatable(self._axes)
cax = divider.append_axes("right", size="1%", pad=0.1)
self._colorbar = self._figure.colorbar(pcolormesh, cax=cax, label="Slope (%)")

I am getting a strange behavior using the NavigationToolbar:

I will open a new thread for this issue.

1 Like