Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 243
DurgaDatta Avatar asked Dec 05 '13 07:12

DurgaDatta


People also ask

How do I swap axes in Matplotlib?

To switch the axes of the plot, set x_data and y_data of the axis 1 curve to axis 2 curve. Adjust the padding between and around the subplots. To display the figure, use show() method.

What does PLT axis () do?

The plt. axis() method allows you to set the x and y limits with a single call, by passing a list which specifies [xmin, xmax, ymin, ymax] : In [11]: plt.

Can scatter plots have negative numbers?

Generally, the scatter plot is plotted on the positive values, but what happens when the dataset has both negative and positive values. So in this article, we are creating the scatter plot with both negative and positive axes.


1 Answers

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

like image 91
mgilson Avatar answered Sep 18 '22 20:09

mgilson