Search code examples
pythonmatplotlibkde-plasma

Modify matplotlib y values after plotting


I am using matplotlib to plot a KDE. The issue with this is I'm using ax.imshow, which only takes in a grid and an extent. Meaning it will linearly space the data from the grid using the minimum value in the extent to the maximum.

I need to flip the y axes (doable), but then I also need to arbitrarily scale every y point by a function, not just a simple factor. As an example, if the y value is 5, I need it to be mapped to f(5)=13 and 6 should be mapped to f(6)=22. The point is, it's not a simple scale.

Is there any way of modifying these y values after they have been plotted, or am I forced to modify these values before-hand.


Solution

  • I couldn't find a way to modify the y values, but I could find a way to modify the labels of the yaxis.

    By scaling the yaxes with the desired function and its inverse, using the following:

    ax.set_yscale('function', **{'functions':[forward, inverse]})

    Then scaling the axes ytick values by the desired function and setting the ytick labels to be the scaled yticks, you effectively scale the y values:

    ax.set_yticklabels(forward(ax.get_yticks()))