I have a scatter plot with logarithmic x- and y axis (because I'm mainly interested in the lower values along both).
However, I want the tick labels to be in decimal format, not as 10^x.
I'm using this:
# axis limits:
ax.set_xlim(xmin=0, xmax = 1.2)
ax.set_ylim(ymin=0,ymax=1000)
# log scales:
ax.set_yscale('log')
ax.set_xscale('log')
# set y-ticks:
ax.set_yticks((1,10,100,1000))
ax.set_yticklabels(("1","10","100","1000"))
This works (though introducing ax.set_yscale('log')
or ax.set_xscale('log')
brings up the following warning (any idea what's up with that?):
Warning (from warnings module):
File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 3785
warnings.warn("Warning: converting a masked element to nan.")
UserWarning: Warning: converting a masked element to nan.
But when I try the same for the x-axis, I get a MaskError:
# set x-ticks:
ax.set_xticks((0, 0.2, 0.4, 0.8, 1))
ax.set_xticklabels(("0", "0.2", "0.4", "0.8", "1"))
[snip long long traceback]
File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 3795, in __int__
raise MaskError, 'Cannot convert masked element to a Python int.'
MaskError: Cannot convert masked element to a Python int.
I think it has something to do with minor vs major ticks. I have tried to play around with ticker, but always run into the same error in the end.
I'd be immensely grateful for any help!
Edit after answer: Problem solved by replacing
ax.set_yticks((1,10,100,1000))
ax.set_yticklabels(("1","10","100","1000"))
ax.set_xticks((0, 0.2, 0.4, 0.8, 1))
ax.set_xticklabels(("0", "0.2", "0.4", "0.8", "1"))
with
ax.yaxis.set_major_formatter(FormatStrFormatter('%1.0f'))
ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.xaxis.set_minor_formatter(FormatStrFormatter('%.1f'))
You can use ticker
as:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
fig = plt.figure(1, [5,4])
ax = fig.add_subplot(111)
ax.plot( range(1,100) , range(1,100) , color='#aaaaff')
ax.set_xscale('log')
ax.xaxis.set_major_formatter(FormatStrFormatter('%.03f'))
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With