0

I'm trying to plot a histogram with the ticklabel_format style as scientific. But, I want the tick mark to appear as a power of 10, not 1e-2. Is there any way I can make the 1e-2 appear as 10^-2?

enter image description here

P.S.: I'm using this for the first time. So please point out any mistakes or ways to improve my code. Thanks in advance.

Code:

import matplotlib.pyplot as plt
time0=[]
with open("dist20_M500.dat") as f:
for line in f:
time0.append(float(line))
num_bins = 30
# the histogram of the data
n, bins, patches = plt.hist(time0, num_bins,  density = True, 
edgecolor='black', histtype='bar',facecolor='red', label = '$N=20$')
plt.xlabel(r'Time', fontsize=18)
plt.ylabel(r'$P(t)$', fontsize=18)
plt.legend()
plt.xticks(fontsize=16)
plt.yticks(fontsize=16) 
plt.ticklabel_format(style='sci', axis='both', scilimits=   
(0,0),fontsize=20)
plt.ticklabel_format(useOffset=False)
cosmos
  • 11
  • 1
  • 3
  • To get rid of the `1e-2` use `plt.ticklabel_format(useOffset=False)`. See [this post](https://stackoverflow.com/questions/18209462/is-ticklabel-format-broken). – JohanC Feb 06 '20 at 17:56
  • 1
    To get powers of 10 use `logfmt = matplotlib.ticker.LogFormatterExponent(base=10.0, labelOnlyBase=True); ax.xaxis.set_major_formatter(logfmt)`. See [this post](https://stackoverflow.com/a/47292281/12046409). – JohanC Feb 06 '20 at 17:59
  • @JohanC It's not working. I want the 1e-2 as 10^-2. – cosmos Feb 06 '20 at 18:33
  • @JohanC I've added the code now. – cosmos Feb 06 '20 at 19:09
  • You wrote that you wanted the 1e-2 as 10^-2, but you set `useOffset=False`? Anyway, you probably also want to set `useMathText=True` in `ticklabel_format` – JohanC Feb 06 '20 at 19:25
  • @JohanC Thanks for pointing out the mistake. useMathText=True actually solved the problem. Thank you very much for your time. – cosmos Feb 06 '20 at 19:29
  • @JohanC There is a little problem. The x10^-3 appears very small. any idea how to change the size? – cosmos Feb 06 '20 at 19:31
  • https://stackoverflow.com/questions/34227595/how-to-change-font-size-of-the-scientific-notation-in-matplotlib – JohanC Feb 06 '20 at 19:43
  • @JohanC Thanks a lot. – cosmos Feb 06 '20 at 19:54

1 Answers1

1

You need to use xscale or yscale and then fix the ticks

plt.xscale('symlog')

Checkout the example here: How to exponentially scale the Y axis with matplotlib

S. Nick
  • 12,879
  • 8
  • 25
  • 33