So, I have 2 problems:
1- I have a histogram of 2D histograms with 1D histograms along the x and y axes. These histograms summarize their respective x and y values, and the main histogram summarizes the values ββin the log x-y-bins. The code is below. I used pcolormesh to create a 2D histogram ... and I created a color bar in the range vmin = 1, vmax = 14 ... I do these constants, since I generate a set of these graphs over a wide range of data - and I want to colors were consistent with each other.
I also want to color 1D histograms according to the same normalization. I configured the function to perform the mapping, but it is stubbornly linear - even if I set LogNorm to display.
I have attached a couple of plots that show what I consider to be a linear scale for 1D histograms. Look at the values ββof the histogram along the x axis about 10 ^ 4 (or 10 ^ 6) ... They are painted at a 1/2 axis point in the color bar, and not on the scale.
What am I doing wrong?
2- I also want to ultimately normalize 1D histograms across the width of the cell (xrange or yrange). However, I do not think I can do this directly in matplotlib.hist. Maybe I need to use np hist, but then I donβt know how to plot the matplotlib.bar graph with the log scale and color bars (again, matching the colors that I use for the 2D histogram).
Here is the code:
def colorHistOnHeight(N, patches):
print("N max: %.2lf"%N.max())
fracs = np.log10(N.astype(float))/9.0
print("fracs max: %.2lf"%fracs.max())
norm = mpl.colors.LogNorm(2.0, 9.0)
for thisfrac, thispatch in zip(fracs, patches):
color = mpl.cm.jet(thisfrac)
thispatch.set_facecolor(color)
return
def genDensityPlot(x, y, mass, pf, z, filename, xaxislabel):
"""
:rtype : none
"""
nullfmt = NullFormatter()
fig = plt.figure(figsize=(20, 20))
ax2dhist = plt.axes(rect_2dhist)
axHistx = plt.axes(rect_histx)
axHisty = plt.axes(rect_histy)
x[x == np.inf] = 0.0
y[y == np.inf] = 0.0
y[y > 1.0] = 1.0
xrange = np.logspace(minX,maxX,xbins)
yrange = np.logspace(minY,maxY,ybins)
H, xedges, yedges = np.histogram2d(y, x, weights=mass * (1.0 - pf),
bins=(yrange,xrange))
extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]
X,Y=np.meshgrid(xrange,yrange)
H = np.log10(H)
masked_array = np.ma.array(H, mask=np.isnan(H))
cmap = copy.copy(mpl.cm.jet)
cmap.set_bad('w', 1.)
cax = (ax2dhist.pcolormesh(X,Y,masked_array, cmap=cmap, norm=LogNorm(vmin=1,vmax=8)))
print("Normalized H max %.2lf"%masked_array.max())
cbar = fig.colorbar(cax, ticks=[1, 2, 4, 6, 8])
cbar.ax.set_yticklabels(['1', '2', '4', '6', '8'], size=24)
cbar.set_label('$log\, M_{sp, pol,\odot}$', size=30)
ax2dhist.tick_params(axis='x', labelsize=22)
ax2dhist.tick_params(axis='y', labelsize=22)
ax2dhist.set_xlabel(xaxislabel, size=30)
ax2dhist.set_ylabel('$log\, Z_{pri}/Z$', size=30)
ax2dhist.set_xlim([10**minX,10**maxX])
ax2dhist.set_ylim([10**minY,10**maxY])
ax2dhist.set_xscale('log')
ax2dhist.set_yscale('log')
ax2dhist.grid(color='0.75', linestyle=':', linewidth=2)
ylims = ax2dhist.get_ylim()
xlims = ax2dhist.get_xlim()
N, bins, patches = axHistx.hist(x, bins=xrange, log=True, weights=mass * (1.0 - pf))
axHistx.set_xscale("log")
colorHistOnHeight(N, patches)
N, bins, patches = axHisty.hist(y, bins=yrange, log=True, weights=mass * (1.0 - pf),
orientation='horizontal')
axHisty.set_yscale('log')
colorHistOnHeight(N, patches)
axHistx.set_xlim(ax2dhist.get_xlim())
axHistx.set_ylim([100.0,10.0**9])
axHistx.tick_params(labelsize=22)
axHistx.yaxis.set_ticks([1e2,1e4,1e6,1e8])
axHistx.grid(color='0.75', linestyle=':', linewidth=2)
axHisty.set_xlim([100.0,10.0**9])
axHisty.set_ylim([10**minY,10**maxY])
axHisty.tick_params(labelsize=22)
axHisty.xaxis.set_ticks([1e2,1e4,1e6,1e8])
axHisty.grid(color='0.75', linestyle=':', linewidth=2)
axHistx.xaxis.set_major_formatter(nullfmt)
axHisty.yaxis.set_major_formatter(nullfmt)
if z[0] == '0': z = z[1:]
axHistx.set_title('z=' + z, size=40)
plt.savefig(filename + "-z_" + z + ".png", dpi=fig.dpi)
plt.close(fig)
return
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
from matplotlib.colors import LogNorm
from matplotlib.ticker import LogFormatterMathtext
import numpy as np
import copy as copy
files = [
"18.00",
"17.00",
"16.00",
"15.00",
"14.00",
"13.00",
"12.00",
"11.00",
"10.00",
"09.00",
"08.50",
"08.00",
"07.50",
"07.00",
"06.50",
"06.00",
"05.50",
"05.09"
]
left, width = 0.1, 0.63
bottom, height = 0.1, 0.63
bottom_h = left_h = left + width + 0.01
xbins = ybins = 100
rect_2dhist = [left, bottom, width, height]
rect_histx = [left, bottom_h, width, 0.15]
rect_histy = [left_h, bottom, 0.2, height]
prefix = "./"
for indx, z in enumerate(files):
spZ = np.loadtxt(prefix + "spZ_" + z + ".txt", skiprows=1)
spPZ = np.loadtxt(prefix + "spPZ_" + z + ".txt", skiprows=1)
spPF = np.loadtxt(prefix + "spPPF_" + z + ".txt", skiprows=1)
spMass = np.loadtxt(prefix + "spMass_" + z + ".txt", skiprows=1)
print ("Generating phase diagram for z=%s" % z)
minY = -4.0
maxY = 0.5
minX = -8.0
maxX = 0.5
genDensityPlot(spZ, spPZ / spZ, spMass, spPF, z,
"Z_PMassZ-MassHistLogNorm", "$log\, Z_{\odot}$")
minX = -5.0
genDensityPlot((spZ) / (1.0 - spPF), spPZ / spZ, spMass, spPF, z,
"Z_PMassZ1-PGF-MassHistLogNorm", "$log\, Z_{\odot}/f_{pol}$")
Here is a couple of graphs showing the problem with coloring the histograms of the 1st axis