-1

I am making subplots in Python of 6 images and the y axis values are very high (range from 0 to 250 000). I want to show them in multiples of 1000, but not that they show up as 1K,10K,250K etc., but as 1,10,250 and at the top there would be 1e3 to show that all the values are multiples of 1000. Is there a simple way to do this? I am doing this to conserve horizontal space because I need it to fit in my dissertation. Here's my output,here's an example of what I want it to look like and below is the code:

fig,ax = plt.subplots(3,2,figsize=(10,20))

im1 = ax[0,0].hist(im1,bins=300,range=(0.66,0.95))

im2 = ax[0,1].hist(im2,bins=300,range=(0,0.8))

im3 = ax[1,0].hist(im3,bins=300,range=(200,280))

im4 = ax[1,1].hist(im4,bins=300,range=(0,80))

im5 = ax[2,0].hist(im5,bins=300,range=(200,280))   

im6 = ax[2,1].hist(im6,bins=300,range=(0,80))

1 Answers1

0

You just have to change the yticklabels to be a specific string format. I've shown it for some dummy data below:

dat = np.random.rand(100000,1)
f,ax = plt.subplots(1,1)
ax.hist(dat) 
pow10 = 3 
ax.set_yticklabels(['{:d}e{:d}'.format(int(i)//(10**pow10),pow10) 
                    if int(i)>(10**pow10) else i for i in ax.get_yticks()])

This might be ugly if they're not even powers of ten, but in that case you can simply choose to edit the yticks themselves as well.

EDIT

To go with your updated question, you can simply change the format of the ticklabels as such:

dat = np.random.rand(100000,1)
f,ax = plt.subplots(1,1)
ax.hist(dat)  
ax.ticklabel_format(axis='y',style='sci',scilimits=(0,0))

Which will produce axes like this:

enter image description here

jhso
  • 1,123
  • 3
  • 7