Hello,

Please consider my attached examples. hw3.py nicely produces multiple axes
electromagnetic spectrum with solar and terrestrial radiation plotted using
Planck's function. However, the major and minor ticks placements are not
nice in those plots and I have decided to use the new axis_grid1 interface.
In the original hw3.py I do multiple plots of desired unit (e.g. wavelength
to wavenumber etc...) I am quite sure that the units shown in the hw3.py are
correctly converted. Now I am planning to produce these conversions with mpl
transformations. Then test1-hw3.py comes into view once again.

Can someone possibly (JJ) give little hint here as to converting from
wavelength to wavenumber using Affine2D and correctly see the ticks located
as the main x-axis.

I do lots of extra lines to get this one working properly in the hw3.py as
shown below:

# multi x-axes
# first wavenumber in cm^-1
parx = ax1.twiny()
parx.axis["top"].set_visible(False)
offset = 0, -60
new_axisline = parx.get_grid_helper().new_fixed_axis
parx.axis["bottom"] = new_axisline(loc="bottom", axes=parx, offset=offset)
parx.axis["bottom"].label.set_visible(True)
parx.axis["bottom"].label.set_text(u"Wavenumber, cm⁻¹")
parx.axis["bottom"].label.set_fontsize(16)
line1, = parx.plot(1/(wavelength[1:]*100), np.ones(len(wavelength[1:])))
line1.set_visible(0)
parx.set_xlim(xmin=1e10,xmax=1/1e6)
parx.set_xscale('log')

Thanks.

-- 
Gökhan
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# ATSC 5003 - Radiation HW 3

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.parasite_axes import SubplotHost
from matplotlib.transforms import Affine2D

plt.close('all')
fig1 = plt.figure(figsize=(11, 8.5))
ax1 = SubplotHost(fig1, 111)
fig1.add_subplot(ax1)

# Approximate surface temperatures of bodies [K]
temp_earth = 288
temp_sun = 5800

# Define constants
pi = np.pi
c = 299792458.0     # speed of light [m/s]
h = 6.62606896e-34  # Planck constant [J*s]
k = 1.3806504e-23   # Boltzmann constant [J/K]
eV = 1.602e-19      # 1 eV in J
wavelength = np.linspace(0., 1e-3, 100000)  # [m]

# Planck's function in wavelength domain (in spectral density form)
def Plancks_wavelength(wavelength, temperature):
    return (8 * pi * h * c) / (wavelength**5 * (np.exp((h * c)/ (k * temperature * wavelength))-1)) 

# Plotting as *irradiance* --hence the c/4 normalization at the top of the atmosphere
ax1.plot(wavelength[1:]*1e6, Plancks_wavelength(wavelength[1:], temp_earth)*c/4, 'k--')
ax1.plot(wavelength[1:]*1e6, Plancks_wavelength(wavelength[1:], temp_sun)*(6.96e8/1.496e11)**2*c/4, 'k-.')
ax1.set_yscale('log')
ax1.set_xscale('log')
ax1.set_ylim(ymin=1e1, ymax=1e10)
ax1.set_xlim(xmin=1e-6, xmax=1e10)
ax1.xaxis.set_label_text(u"Wavelength, μm")
ax1.yaxis.set_label_text(u"Irradiance, W / m²·m")
ax1.axis["left"].label.set_fontsize(16)
ax1.axis["bottom"].label.set_fontsize(16)

ax1.plot(np.linspace(1e-6, 3e-5, 100), 1e9*np.ones(len(np.linspace(1e-6, 3e-5, 100))), "k-", label="Gamma", lw=4.0)
ax1.plot(np.linspace(4e-5, 4e-3, 100), 1e8*np.ones(len(np.linspace(3e-5, 4e-3, 100))), "k-", label="X-Ray", lw=3.5)
ax1.plot(np.linspace(1e-2, 3e-1, 100), 1e7*np.ones(len(np.linspace(1e-2, 4e-1, 100))), "k-", label="UV", lw=3.0)
ax1.plot(np.linspace(4e-1, 7e-1, 100), 1e6*np.ones(len(np.linspace(4e-1, 7e-1, 100))), "k-", label="Vis", lw=2.5)
ax1.plot(np.linspace(8e-1, 3e2, 100), 1e5*np.ones(len(np.linspace(7e-1, 3e2, 100))), "k-", label="IR", lw=2.0)
ax1.plot(np.linspace(3e4, 2e6, 100), 1e4*np.ones(len(np.linspace(3e4, 3e6, 100))), "k-", label="Microwave", lw=1.5)
ax1.plot(np.linspace(3e6, 3e9, 100), 1e3*np.ones(len(np.linspace(3e6, 3e9, 100))), "k-", label="Radio", lw=1)
ax1.legend(frameon=0)


# multi x-axes
# first wavenumber in cm^-1
parx = ax1.twiny()
parx.axis["top"].set_visible(False)
offset = 0, -60
new_axisline = parx.get_grid_helper().new_fixed_axis
parx.axis["bottom"] = new_axisline(loc="bottom", axes=parx, offset=offset)
parx.axis["bottom"].label.set_visible(True)
parx.axis["bottom"].label.set_text(u"Wavenumber, cm⁻¹")
parx.axis["bottom"].label.set_fontsize(16)
line1, = parx.plot(1/(wavelength[1:]*100), np.ones(len(wavelength[1:])))
line1.set_visible(0)
parx.set_xlim(xmin=1e10,xmax=1/1e6)
parx.set_xscale('log')


# second frequency in s^-1
parx2 = ax1.twiny()
parx2.axis["top"].set_visible(False)
offset = 0, -120
new_axisline = parx2.get_grid_helper().new_fixed_axis
parx2.axis["bottom"] = new_axisline(loc="bottom", axes=parx2, offset=offset)
parx2.axis["bottom"].label.set_visible(True)
parx2.axis["bottom"].label.set_text(u"Frequency, s⁻¹")
parx2.axis["bottom"].label.set_fontsize(16)
line2, = parx2.plot(c/(wavelength[1:]), np.ones(len(wavelength[1:])))
line2.set_visible(0)
parx2.set_xlim(xmin=c/(1e-6*1e-6),xmax=c/(1e10*1e-6))
parx2.set_xscale('log')


# third energy in J
parx3 = ax1.twiny()
parx3.axis["top"].set_visible(False)
offset = 0, -180
new_axisline = parx3.get_grid_helper().new_fixed_axis
parx3.axis["bottom"] = new_axisline(loc="bottom", axes=parx3, offset=offset)
parx3.axis["bottom"].label.set_visible(True)
parx3.axis["bottom"].label.set_text(u"Energy, J")
parx3.axis["bottom"].label.set_fontsize(16)
line3, = parx3.plot(h*c/(wavelength[1:]), np.ones(len(wavelength[1:])))
line3.set_visible(0)
parx3.set_xlim(xmax=h*c/(1e10*1e-6),xmin=h*c/(1e-6*1e-6))
parx3.set_xscale('log')


# fourth energy in eV
parx4 = ax1.twiny()
parx4.axis["top"].set_visible(False)
offset = 0, -240
new_axisline = parx4.get_grid_helper().new_fixed_axis
parx4.axis["bottom"] = new_axisline(loc="bottom", axes=parx4, offset=offset)
parx4.axis["bottom"].label.set_visible(True)
parx4.axis["bottom"].label.set_text(u"Energy, eV")
parx4.axis["bottom"].label.set_fontsize(16)
line4, = parx4.plot((h*c/(wavelength[1:]))/eV, np.ones(len(wavelength[1:])))
line4.set_visible(0)
parx4.set_xlim(xmax=(h*c/(1e10*1e-6))/eV,xmin=(h*c/(1e-6*1e-6))/eV)
parx4.set_xscale('log')
ax1.set_ylim(ymin=1e1, ymax=1e10)

# second figure
fig2 = plt.figure(figsize=(11, 8.5))
ax2 = SubplotHost(fig2, 111)
fig2.add_subplot(ax2)

freq = np.linspace(1e8, 1e16, 1e5)  # [1/s]
# Planck's function in frequency domain (in spectral density form)
def Plancks_freq(freq, temperature):
    return (8 * pi * h * freq**3) / (c**3 * (np.exp((h * freq)/ (k * temperature))-1))

ax2.plot(freq, Plancks_freq(freq, temp_earth)*c/4, 'k--')
ax2.plot(freq, Plancks_freq(freq, temp_sun)*(6.96e8/1.496e11)**2*c/4, 'k-.')
ax2.set_yscale('log')
ax2.set_xscale('log')
ax2.set_ylim(ymin=1e-19, ymax=1e-10)
ax2.set_xlim(xmin=1.e5, xmax=1.e21)
ax2.xaxis.set_label_text(u"Frequency, s⁻¹", fontsize=16)
ax2.yaxis.set_label_text(u"Irradiance, W / m²·s", fontsize=16)
ax2.axis["left"].label.set_fontsize(16)
ax2.axis["bottom"].label.set_fontsize(16)


# multi x-axes
# first wavelength in um
parx = ax2.twiny()
parx.axis["top"].set_visible(False)
offset = 0, -60
new_axisline = parx.get_grid_helper().new_fixed_axis
parx.axis["bottom"] = new_axisline(loc="bottom", axes=parx, offset=offset)
parx.axis["bottom"].label.set_visible(True)
parx.axis["bottom"].label.set_text(u"Wavelength, μm")
parx.axis["bottom"].label.set_fontsize(16)
line1, = parx.plot((c/freq)*1e6, 1e-19*np.ones(len(c/freq)))
line1.set_visible(0)
parx.set_xlim(xmin=(c/1e5)*1e6,xmax=(c/1e21)*1e6)
parx.set_xscale('log')

# second wavenumber in cm^-1
parx2 = ax2.twiny()
parx2.axis["top"].set_visible(False)
offset = 0, -120
new_axisline = parx2.get_grid_helper().new_fixed_axis
parx2.axis["bottom"] = new_axisline(loc="bottom", axes=parx2, offset=offset)
parx2.axis["bottom"].label.set_visible(True)
parx2.axis["bottom"].label.set_text(u"Wavenumber, cm⁻¹")
parx2.axis["bottom"].label.set_fontsize(16)
line2, = parx.plot(1/((c/freq)*100), np.ones(len(c/freq)))
line2.set_visible(0)
parx2.set_xlim(xmin=1/((c/1e5)*100),xmax=1/((c/1e21)*100))
parx2.set_xscale('log')
ax2.set_ylim(ymin=1e-19, ymax=1e-10)

# third energy in J
parx3 = ax2.twiny()
parx3.axis["top"].set_visible(False)
offset = 0, -180
new_axisline = parx3.get_grid_helper().new_fixed_axis
parx3.axis["bottom"] = new_axisline(loc="bottom", axes=parx3, offset=offset)
parx3.axis["bottom"].label.set_visible(True)
parx3.axis["bottom"].label.set_text(u"Energy, J")
parx3.axis["bottom"].label.set_fontsize(16)
line3, = parx3.plot(h*freq, np.ones(len(h*freq)))
line3.set_visible(0)
parx3.set_xlim(xmin=h*1e5, xmax=h*1e21)
parx3.set_xscale('log')
ax2.set_ylim(ymin=1e-19, ymax=1e-10)

# fourth energy in eV
parx4 = ax2.twiny()
parx4.axis["top"].set_visible(False)
offset = 0, -240
new_axisline = parx4.get_grid_helper().new_fixed_axis
parx4.axis["bottom"] = new_axisline(loc="bottom", axes=parx4, offset=offset)
parx4.axis["bottom"].label.set_visible(True)
parx4.axis["bottom"].label.set_text(u"Energy, eV")
parx4.axis["bottom"].label.set_fontsize(16)
line4, = parx4.plot(h*freq/eV, np.ones(len(h*freq)))
line4.set_visible(0)
parx4.set_xlim(xmin=h*1e5/eV, xmax=(h*1e21/eV))
parx4.set_xscale('log')
ax2.set_ylim(ymin=1e-19, ymax=1e-10)


ax2.plot(np.linspace(1e5, 1e8, 100), 1e-17*np.ones(len(np.linspace(1e5, 1e8, 100))), "k-", label="Radio", lw=1.0)
ax2.plot(np.linspace(1e8, 1e10, 100), 1e-16*np.ones(len(np.linspace(1e8, 1e10, 100))), "k-", label="Microwave", lw=1.5)
ax2.plot(np.linspace(1e12, 3e14, 100), 1e-15*np.ones(len(np.linspace(1e12, 4e14, 100))), "k-", label="IR", lw=2.0)
ax2.plot(np.linspace(4e14, 8e14, 100), 1e-14*np.ones(len(np.linspace(4e14, 8e14, 100))), "k-", label="Vis", lw=2.5)
ax2.plot(np.linspace(9e14, 4e16, 100), 1e-13*np.ones(len(np.linspace(8e14, 4e16, 100))), "k-", label="UV", lw=3.0)
ax2.plot(np.linspace(1e17, 8e18, 100), 1e-12*np.ones(len(np.linspace(1e17, 1e19, 100))), "k-", label="X-Ray", lw=3.5)
ax2.plot(np.linspace(1e19, 1e21, 100), 1e-11*np.ones(len(np.linspace(1e19, 1e21, 100))), "k-", label="Gamma", lw=4.0)
ax2.legend(frameon=0, loc='upper left')

fig1.subplots_adjust(bottom=0.50, top=0.94, right=0.98, left=0.08)
fig2.subplots_adjust(bottom=0.50, top=0.94, right=0.98, left=0.08)
fig1.suptitle("Maxwell's Rainbow (Wavelength domain)", x=0.53, y=0.98, fontsize=18)
fig2.suptitle("Maxwell's Rainbow (Frequency domain)", x=0.53, y=0.98, fontsize=18)
fig1.savefig("plot1.pdf")
fig2.savefig("plot2.pdf")
plt.show()
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# ATSC 5003 - Radiation HW 3

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker
import mpl_toolkits.axes_grid1 as axes_grid1
from matplotlib.transforms import Affine2D

plt.close('all')
fig1 = plt.figure(figsize=(11, 8.5))
ax1 = axes_grid1.host_subplot(211)

# Approximate surface temperatures of bodies [K]
temp_earth = 288
temp_sun = 5800

# Define constants
pi = np.pi
c = 299792458.0     # speed of light [m/s]
h = 6.62606896e-34  # Planck constant [J*s]
k = 1.3806504e-23   # Boltzmann constant [J/K]
eV = 1.602e-19      # 1 eV in [J]
wavelength = np.linspace(7.4e-8, 1e-3, 40000)  # [m]

# Planck's function in wavelength domain (in spectral density form)
def Plancks_wavelength(wavelength, temperature):
    return (8 * pi * h * c) / (wavelength**5 * (np.exp((h * c)/ (k * temperature * wavelength))-1)) 

# Plotting as *irradiance* --hence the c/4 normalization, at the top of the atmosphere
ax1.plot(wavelength*1e6, Plancks_wavelength(wavelength, temp_earth)*(6371e3/(6371e3+150e3))**2*c/4, 'k-', lw=1.5)
ax1.plot(wavelength*1e6, Plancks_wavelength(wavelength, temp_sun)*(6.96e8/1.496e11)**2*c/4, 'k-', lw=2.5)
ax1.set_yscale('log')
ax1.set_xscale('log')
ax1.set_ylim(ymin=1e1, ymax=1e10)
ax1.set_xlim(xmin=1e-7, xmax=1e10)
xmin, xmax = ax1.xaxis.get_view_interval()
#ax1.xaxis.set_ticks(10.**np.arange(np.log10(xmin),np.log10(xmax)+1,1))
ax1.xaxis.set_minor_locator(ticker.LogLocator(subs=np.arange(2.0, 10.0)))
ax1.yaxis.set_minor_locator(ticker.LogLocator(subs=np.arange(2.0, 10.0)))
ax1.xaxis.set_label_text(u"Wavelength, μm", fontsize=16)
ax1.yaxis.set_label_text(u"Irradiance, W / m²·m", fontsize=16)
for ticklabel in ax1.xaxis.get_majorticklabels():
    ticklabel.set_weight('bold')
for ticklabel in ax1.yaxis.get_majorticklabels():
    ticklabel.set_weight('bold')
ax1.grid(1)

# Adding multiple axes
#trans1 = Affine2D().scale(1., 1.)
#parx1 = ax1.twin(trans1)
parx1 = ax1.twin()

#parx1.axis["right"].toggle(ticklabels=False)
parx1.axis["right"].toggle(all=0)
#parx1.axis["top"].toggle(ticklabels=False)
parx1.axis["top"].toggle(all=0)
parx1.axis["left"].toggle(all=0)
parx1.axis["bottom"].toggle(ticklabels=True, label="Wavenumber")
parx1.axis["bottom"].line.set_visible(True)
parx1.spines["bottom"].set_position(('outward',60))
parx1.set_xscale('log')


plt.tight_layout()
plt.show()
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to