简体   繁体   English

如何用最后一行/列和对数轴绘制matplotlib pcolor?

[英]How to plot matplotlib pcolor with last row/column AND logarithmic axis?

I've been trying to plot a 2D histogram-like dataset that's pre-calculated. 我一直在尝试绘制预先计算的类似于2D直方图的数据集。 I have to plot a logarithmic x-axis against a linear y-axis with a linear/logarithmic z-axis. 我必须将对数x轴相对于线性y轴与线性/对数z轴作图。 However, pcolor drops the last row and column, which is a problem since the highest energy value is imperative to plot. 但是,pcolor会删除最后一行和最后一列,这是一个问题,因为必须绘制最高能量值。 Imshow just doesn't work with a logarithmic axis. Imshow不适用于对数轴。

I'm looking at having to pad the array with NaNs to plot properly. 我正在考虑必须用NaN填充阵列以正确绘制。 Is there a pure plotting routine I can use? 我可以使用纯绘图程序吗? Thanks. 谢谢。

Sample code: 样例代码:

    # 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()

First question: You need to give the edges of the meshes, so that the dimensions of x and y are one bigger than z. 第一个问题:您需要给网格的边,使xy的尺寸比z大一。

Second Question: You need to give an instance of matplotlib.colors.LogNorm to the norm keyword. 第二个问题:您需要给norm关键字提供一个matplotlib.colors.LogNorm实例。

Here a simplified example: 这里是一个简化的例子:

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')

And the resulting image: 以及得到的图像: 结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM