Reputation: 165
I have a plot that is a little complex, where I have two X-axes (one on top, one on bottom) and one Y-axis, all in log-log space. And while I'm not saying this is the most efficient way to do it, this is how it's working right now:
Jy = (.15E-5)
D = [11.4,2.7,3.,9.,8.,8.,13.6,10.,16.6,4.3,14.7,2.7,13.5,18.,9.9]
years = [1994,1972,1937,1989,1981,1937,1971,1960,1965,1974,1960,1895,1939,1983,1998]
names= ['SN1994D','SN1972E','SN1937C','SN1989B','SN1981B','SN1937D','SN1971I','SN1960F','SN1965I','SN1974G','SN1960R','SN1895B','SN1939A','SN1983U','SN1998bu']
Sv = Jy*10E-23
Lv = [4*np.pi*((i*3.086e+24)**2)*Sv for i in D]
def two_scales(ax1, data1, data2,lum, c1, c2):
ax2 = ax1.twiny()
ax1.loglog(data1, lum, color=c1)
ax1.set_xlabel('Time (years)')
ax1.set_ylabel('Luminosity (erg/s/Hz)')
ax2.loglog(data2, lum, color=c2)
ax2.set_xlabel('Radius (cm)')
return ax1, ax2
# Create axes
fig, ax = plt.subplots()
ax1, ax2 = two_scales(ax, tim, rad*3.086e+18, lum, 'g', 'w')
ax1.loglog(tim3, lum3, 'g', label= "n=.1 cm^-3")
ax1.loglog(tim, lum, 'b', label= "n=1 cm^-3")
ax1.loglog(tim10, lum10, 'r', label="n= 10 cm^-3")
ax1.legend(loc=3)
ax1.set_xlim(20,1.5E2)
ax1.set_ylim(1E23, 5E25)
ax1.scatter(time, Lv)
ax1.get_xaxis().set_major_formatter(ScalarFormatter())
ax1.set_xticks([20, 30, 40, 60, 100])
for i, txt in enumerate(names):
ax1.annotate(txt, (time[i],Lv[i]))
plt.savefig('lumin-rad.png',dpi=600, bbox_inches='tight')
The issue I get is I would ideally like the lower axis for time to not be in log space, but to just say "20, 30, 40, 60, 100" instead of "2x10^1" etc. So I have found two examples online on how to do this, either setting the xticks myself for that axis, or setting the major formatter to scalar. However, if I do the ScalarFormatter only, I get 100 instead of 1x10^2, but the rest of the numbers will still show the exponent. If I do the code without that line, it doesn't work at all. But if I do both, I will get the correct numbers, but on top I'll get the exponents showing that I don't want!
Does anyone know how to fix this?
Upvotes: 0
Views: 899
Reputation: 40747
Your problem is that you are only changing the display of the major ticks (100) but not the minor ticks. To obtain the desired behavior, change both the major and minor formatter.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
fig, ax = plt.subplots()
ax.loglog([20, 1.5e2], [1e23, 5e25])
frmt = ScalarFormatter()
ax.get_xaxis().set_major_formatter(frmt)
ax.get_xaxis().set_minor_formatter(frmt)
plt.show()
PS: please note how I reduced your problem to just a few line of code. See Minimal, Complete, and Verifiable example
Upvotes: 1