Python, matplotlib: how to set tick label values to their logarithmic values -
i have data plot on semi-log plot (log-lin style, logarithmic scale on y-axis). there way change y-axis tick labels actual values logarithmic values?
as example, consider following code:
import matplotlib.pyplot plt import numpy np x=np.array([1,2,3,4,5]) def f(x): return 10**(x-1) plt.plot(x,f(x)) plt.yscale(u'log') plt.show()
which produces following plot:
(sorry kind of big, not know how make smaller, feel free edit out that).
in plot tick labels shown 10^0, 10^1, 10^2, etc.; them display logarithmic values: 0, 1, 2, etc.
i realize go , change plt.plot(x,f(x))
plt.plot(x,np.log10(f(x)))
, make y-axis linear again instead of logarithmic want know if there anyway matplotlib can change y-axis tick values without me having put np.log10()
in plt.plot()
's. reason two-fold: have many plt.plot()
lines in code , rather not go , have change of them, , wouldn't have logarithmically spaced minor ticks (although i'm sure there's way change linear axis).
edit: aware of this question has similarities mine not same. person in question wants change tick labels scientific form "normal" decimal form. want change tick labels scientific form logarithmic (base 10) value of number. sure answer similar 1 linked not obvious me how it. in fact, looked @ question before posting mine still decided post mine because did not know how apply problem. perhaps experienced programmers obvious how apply methods of question linked situation isn't obvious me please step me through it.
if show me code sample (by copying code sample , putting in necessary lines) how works appreciate it.
you can use custom formatter, example:
import matplotlib.pyplot plt matplotlib.ticker import funcformatter import numpy np import math x=np.array([1,2,3,4,5]) def f(x): return 10**(x-1) plt.plot(x,f(x)) plt.yscale(u'log') #set custorm tick formatting plt.gca().yaxis.set_major_formatter(funcformatter(lambda x,y: '{}'.format(math.log(x, 10)))) plt.show()
Comments
Post a Comment