Contour plot cannot be sized to eliminate blank space

Hi, I couldn’t find the answer in the existing documentation, so I would appreciate help with this problem. I am making a 2D contour plot from binned data. I am using CONT4Z to make the plot. When plotted, Root puts white space along the axes. I assume this is because the contour is interpolating between bin contents, and it can’t interpolate to bins outside the range, especially if they are 0.

I tried to be clever and add more bins on all 4 sides of the plot, making each new bin value equal to the value in the neighboring bin (just like an extension of the neighboring bin). When plotted, Root once again places the white space along the axes where there is no bin to compare to.

When I try to force Root to only show me the colored part of the plot (no white space) in the range that I really want, it refuses.

I have tried a few things like
hist->GetXaxis()->SetRangeUser(2.,8.);
but Root doesn’t budge.

In addition, I want to add a Z-axis label along the right side, but in contour mode, Root won’t let me change the size of the plotted area. I usually just grab and drag the right edge of the plotted area over to the left to make room for a Z-axis label. But no go with contour plots. Nothing to drag.

I want the contours to run continuously between the axes from zenith 90-180 deg and log(energy) from 2 to 8.

Here is the clever plot with the necessary information outside the desired range, but no way that I can figure out how to zoom in to only the colored part within my desired range.

Thanks in advance for your help.

These margins show up because of the algorithm used. CONT4 is nothing more than a surface (option SURf3) see from the top, The algorithm drawing surfaces uses the bins centres to build the surface. Therefore half a bin around the surface is not covered by the surface. That’s what you see.

Thanks, I assumed this was the case. Is there any work-around such that I only see the colored area and not the blank area on the sides?

Using CONT4 I do not see any workaround. The best representation doing what you want is the COL option

Create a “variable bin size” histogram with the very first and the very last bins very narrow (along both axes).

Many thanks for the help. I ended up switching to Python (with the help of a colleague who speaks Python fluently) to make the plot that I needed. Best wishes to all.

I do not see why going to python should fix this.

Here is the Python code that my colleague came up with that worked great for my project (with extra characters to identify comments). Perhaps the concept can be used in Root as well:

import matplotlib.pyplot as plt
import numpy as np

###// Load 2D table of flux factors from text file
flux_ratio = np.loadtxt(‘csms10_Nu_weight.txt’)

###// Range of plot
log10emin,log10emax = (2,8)
zmin,zmax = (90,180)

###// Array of energy and zenith bin edges
zbins = np.linspace(zmin,zmax,flux_ratio.shape[0]+1)
ebins = np.logspace(log10emin,log10emax,flux_ratio.shape[1] + 1)

###// Array of bin centers
zcens = (zbins[1:]+zbins[:-1])/2
ecens = np.sqrt(ebins[1:]*ebins[:-1])

###// We only know the attenuation factor at the bin centers, so interpolate to find values at the bin edges
fix_flux_ratio = np.apply_along_axis(lambda f: np.interp(zbins,zcens,f),0,flux_ratio)
fix_flux_ratio = np.apply_along_axis(lambda f: np.interp(np.log10(ebins),np.log10(ecens),f),1,fix_flux_ratio)

###// Array of heights for each contour level
levels = np.linspace(0,1,21)

fig = plt.figure()
###// Docs at http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.contourf.html
plt.contourf(ebins,zbins,fix_flux_ratio,levels,lw=0)
plt.colorbar(label=‘Transmission probability’)
plt.xscale(‘log’)
plt.xlabel(‘Neutrino energy [GeV]’)
plt.ylabel(‘Zenith angle [deg]’)
plt.savefig(‘contour.pdf’,format=‘pdf’,dpi=500)

Ah ok … but you do not use ROOT plotting any more … I understand now…

Also note that in ROOT you use binned data… an histogram … mathplotlib uses arrays … That’s very different !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.