我一直在尝试绘制预先计算出的类似2D直方图的数据集。我必须绘制一个对数x轴对线性y轴与线性/对数z轴。但是,pcolor会丢弃最后一行和一列,这是一个问题,因为最高能量值是必须绘制的。 Imshow不适用于对数轴。
我正在寻找必须用NaN填充数组来正确绘图。我可以使用一个纯粹的绘图程序吗?感谢。
示例代码:
# INITIALIZATION
alpha_bounds = [0.0, 90.0]
alpha_step = 15.0
all_alphas = np.arange(alpha_bounds[0], alpha_bounds[1], alpha_step)
beta_bounds = [0.0, 360.0]
beta_step = 45.0
all_betas = np.arange(beta_bounds[0], beta_bounds[1], beta_step)
energy_bounds = [2e3, 5e6]
n_energies = 15
all_energies = np.logspace(
np.log10(energy_bounds[0]), np.log10(energy_bounds[1]), n_energies)
all_locations =\
[(-52.5, 180.0, r - 1), (-77.5, 260.0, r - 1)]
alts = np.linspace(
70.0, 600.0, 500)
# ARRAY TO GET ELOSS PER ALT, LOC, BETA, ALPHA, ENERGY
# Changed np.zeros to np.ones for testing
eloss_per_alt_per_process = np.ones(
(len(all_locations),
len(all_alphas), len(all_betas), len(all_energies),
len(alts), len(processes)))
# CODE HERE TO COUNT ELOSS PER ALT, LOC, BETA, ALPHA, ENERGY
# SUM OVER TWO AXES
eloss_per_alt_per_process[0, 0, 0, 0,
:, :] = np.sum(eloss_per_alt, axis=(1, 2))
# PLOTTING
ALT, E = np.meshgrid(np.array(all_energies), alts)
eloss = np.transpose(
eloss_per_alt_per_process[0, 0, 0, :, :, 2])
if np.any(eloss):
plt.figure()
plt.pcolor(
ALT, E, eloss) #, norm=LogNorm()) #, vmin=1e-1,
# vmax=ncoll.max())
plt.xscale('log')
plt.show()
答案 0 :(得分:0)
第一个问题:您需要给出网格的边缘,以便 x 和 y 的尺寸比z大。
第二个问题:
您需要为matplotlib.colors.LogNorm
关键字提供norm
的实例。
这是一个简化的例子:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
mean = [0, 0]
cov = [[1, 0.5],
[0.5, 1]]
# draw some random numbers to show in the 2dhist
data = np.random.multivariate_normal(mean, cov, 100000)
# create the histogram
entries, xedges, yedges = np.histogram2d(data[:,0], data[:,1], [10, 10], [[-5, 5], [-5, 5]])
# create all combinations of x & y
x, y = np.meshgrid(xedges, yedges)
# plot, note that x and y are (m + 1) x (n + 1) arrays if entries is n x m
plt.pcolor(x, y, entries, norm=LogNorm(), cmap='afmhot')
plt.colorbar()
plt.tight_layout()
plt.savefig('2hist.png')
结果图片: