0

I'm trying to manipulate my y axis in a barplot. Say I have this code

import numpy as np
import matplotlib.pyplot as plt
N = 11
men_means = (-500,0,1,2,5,10,20,50,100,200,500)
men_std = (-500,0,1,2,5,10,20,50,100,200,500)

ind = np.arange(N)  # the x locations for the groups
width = 0.25       # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(ind, men_means, width, color='r', yerr=men_std)

women_means = (-500,0,1,2,5,10,20,50,100,200,500)
women_std = (-500,0,1,2,5,10,20,50,100,200,500)
rects2 = ax.bar(ind + width, women_means, width, color='y', yerr=women_std)

ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7'))

ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))

plt.show()

It will give me a y axis that is linear. Of course I could use 'symlog' to make the y-axis logarithmic (for my original data there are negative values as well) but what I really want is to have a 'pseudo logarithmic y axis', where the ticks are set to -500, -200, -100, -50, [..],0, 1, 2 ,5, 10, 20, [...], 500, but are equally distributed along the yaxis.

Can anyone help? :-)

scriptgirl_3000
  • 161
  • 3
  • 16
  • You can transform the data yourself and create an custom y axis (e.g. following example in https://matplotlib.org/examples/pylab_examples/custom_ticker1.html). Btw, your code example fails, you should set N to 10 – Sosel Dec 01 '17 at 10:11
  • Yes I just realized and changed the N value – scriptgirl_3000 Dec 01 '17 at 10:15
  • Ok. Well I don't get how I can use this. In the expample you posted the y-axis is exactly how I don't want it to look like. python takes all values for money, makes an y axis from zero to the max value and returns a linear y axis. In that example, I'd much rather have a y axis where my first value would be 1.5e5, then 2.5e6 and after that, in equal distance, 5.5e6 and 2.0e7 – scriptgirl_3000 Dec 01 '17 at 10:29
  • The following 3 steps can bring you to your solution: Step 1, transform the data (i.e. you have to think about a new transformation beside linear and logarithmic, which is difficult already). Step 2, plot the transformed data, and step 3, now you have to re-transform your y-axis ticks (like in the link to the tutorial). – Sosel Dec 01 '17 at 10:37
  • Hmmm. Ok. Thanks! This much more complicated than I thought it's be – scriptgirl_3000 Dec 01 '17 at 10:45
  • Transforming data is of course a possible solution. Normally one would instead create a new axis scaling. But the main point here is that such a scale is not very intuitively understood by any viewer. Especially when there is no way of understanding what value a datapoint midway between two ticks corresponds to, I would even consider such a scale totally useless (If handed in a report with such a graph, I would reject it immediately.) So if this is really what you want to have, [this example](https://matplotlib.org/gallery/api/custom_scale_example.html) would be the place to look at. – ImportanceOfBeingErnest Dec 01 '17 at 10:49
  • Actually, I answered such a question already once: https://stackoverflow.com/questions/45306099/how-to-put-different-in-magnitude-values-at-same-distance-on-x-axis Should we mark this as duplicate? – ImportanceOfBeingErnest Dec 01 '17 at 11:07

0 Answers0