DurgaDatta
DurgaDatta

Reputation: 4170

Convert negative y axis to positive (matplotlib)

I want to plot bar chart for some parameters for men and women. I have done like this: enter image description here

I want to show the frequency for mean in upper side (positive y axis) and for women in lower side (negative x-axis). In this case, for frequency only the magnitude matter and sign does not. Just for convenience I am showing one in upper side and another in lower side. Can I change the labeling (-5, -10, ... here) in negative y-axis so that their magnitude remain same but all are positive (5, 10,...) . Now there should be two positive y-axis , one mirror image of other.

Upvotes: 6

Views: 6981

Answers (1)

mgilson
mgilson

Reputation: 309929

Sure it can be done. Here's an example that you can play around with:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
ax.plot(x,y)
ax.set_yticklabels([str(abs(x)) for x in ax.get_yticks()])
ax.show()

Here I just set the yticklabel to be the absolute value of the y position. Of course, I have symmetric data and you don't. If you want it to be "mirrored" down the middle, you'll need to set an explicit y range with ax.set_ylim

Upvotes: 8

Related Questions