How to query auto set axis bounds in bokeh? - python

I've created a plot and let bokeh choose the axis bounds automatically. Now I want to know what the min and max values of that axis are (specifically because I want to overlay the plot of a mathematical function and want to know over what range to evaluate it).
The obvious approach was to query fig.y_range.start and fig.y_range.end, but it appears those are set to None when autoscaled, so they look like input parameters.
How do I determine what the results of autoscaling were?

This information is not available in Python, those bounds are only computed in the browser, in JavaScript. If you need explicit details/control, you will need to set the ranges manually, e.g.
p = figure(x_range=(0, 137))

Related

Is there a PyQtGraph parameter for autorange which limits how many points are visible?

I have a PyQtGraph (Line graph) which constantly has new values added too it, and I am using the plot.autoRange() function to update the viewBox, but the problem is that I am using custom Ticks (Time, 12:00PM for example), and if it has more than 10-ish values the x-ticks overlap when it auto ranges. Is it possible to for example make autoRange only show the last 10 values?
Currently I found a workaround by removing the first value once 10 has been reached, but this really isn't optimal since the old data isn't in the graph anymore.
Generally speaking if you want that kind of custom behavior, you need to use the setRange method:
https://pyqtgraph.readthedocs.io/en/latest/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setRange
If you want to stick with autoRange, the autoRange method takes an optional items argument; what you can do is create an overlapping curve of just the last 10 points (that you want to display) and call the autoRange function on just that curve, not all the items in the ViewBox. If they overlap entirely it should be visually not noticeable (but if you have mouse related events, you may have more complication).
Hopefully that helps

Why is part of my contour plot showing white?

I am using Python's matplotlib.pyplot.contourf to create a contour plot of my data with a color bar. I have done this successfully countless times, even with other layers of the same variable. However, when the values get small (on the order of 1E-12), parts of the contour show up white. The white color does not show up in the color bar either. Does anyone know what causes this and how to fix this? The faulty contour is attached below.
a1 = plt.contourf(np.linspace(1,24,24),np.linspace(1,20,20),np.transpose(data[:,:,15]))
plt.colorbar(a1)
plt.show()
tl;dr
Given the new information, matplotlib couldn't set the right number of levels (see parameters in the documentation) for your data leaving data unplotted. To fix that you need to tell matplotlib to extend the limits with either plt.contourf(..., extend="max") or plt.contourf(..., extend="both")
Extensive answer
There are a few reasons why contourf() is showing white zones with a colormap that doesn't include white.
NaN values
NaN values are never plotted.
Masked data
If you mask data before plotting, it won't appear in the plot. But you should know if you masked your data.
Although, you may have unnoticed mask your data if you use something like Tick locator = LogLocator().
Matplotlib couldn't set the right levels for your data
Sometimes matplotlib doesn't set the right levels, leaving some of your data without plotting.
To fix that you can user plt.contourf(..., extend=EXTENDS) where EXTENDS can be "neither", "both", "min", "max"
Coarse grid
contourf plots whitespace over finite data. Past answers do not correct
One remark, white section in the plot can also occur if the X and Y vectors data points are not equally spaced. In that case best to use function tricontourf().
I was facing the same problem recently, when there was data available even higher/lower than the levels I have set. So, the plt.contourf fills the contours exclusively given by you, and it neglects any other higher or lower values present in your data.
I solved this by adding a key word argument extend="both", which for your case would be something like this:
a1 = plt.contourf(np.linspace(1,24,24),np.linspace(1,20,20),np.transpose(data[:,:,15]), extend="both")
or in general form:
a1 = plt.contourf(x,y,variable[:,:,15],extend="both")
By doing this, you're instructing the module to plot the higher(/lower) values according to the highest(/lowest) filled contour.
If you want only to extend in the lower or higher range, you can change the keyword argument to
extend="min" or extend ="max"

Plotly autorange axes setting

I have one question for plotly x and y axes setting.
It's possible to autorange the axis only the first time based on all input data and then turn off rescaling while manipulating the data input (in the legend)?
https://plot.ly/python/reference/#layout-xaxis-type
From documentation: 'autorange' default: True
Determines whether or not the range of this axis is computed in relation to the input data.
I need autorange only to be done first time and then it should behave like False. I need it so I can make evaluations relative to the whole dataset.
Maybe it can be done another way, not by manipulating autorange but that's why I'm asking.
MY EXAMPLE:
Imagine you have visualization like this. I have labeled many groups so that I can turn them off/on by plotly functionality. But the problem for me is that it is rescaling everytime based on the input data (only the ones which are 'turned on').
This is after I isolate the GROUP 1. But I want the same x-axis and y-axis as I had before (which was 'autoranged' when I started the visualization).
Thanks for your help!

the axes control of mlab.axes

This is just a small question. I use the sentence below to control the three axes ranges.
mlab.axes(xlabel='x', ylabel='y', zlabel='z',ranges=(0,10000,0,10000,0,22),nb_labels=10)
In fact the real data ranges are (3000,4000),(5000,6000),(0,22) respectively.
However the axes of the figure I plot is scaled to (0,10000,0,10000,0,22).
I did not find a parameter of mlab.axes can control that.
Do I have to calculate the data ranges every time? Without knowing the real data range, is there a way to make the axis range obey the real data?

How to force color mapping to a dynamic range larger than the particular input to imshow in matplotlib

Suppose I want to make 2+ heatmaps (on the same, or different Figures) and have the color<->value mapping be the same among them.
By default, the extreme values in the colormap (say jet) will be used for the dynamic range of each heatmap individually (i.e. each call to imshow), and I'd like to force the mapping to be the same, i.e. use the global dynamic range.
I think an equivalent statement is that I'd like to somehow specify an absolute mapping, whereas the behavior of imshow given a cmap object, is relative to the dynamic range of the input.
If you don't want to specify the ranges but somehow you know that one plot has the largest range, you can get that range with Axesimage.properties()['clim'] and set it for the other plots as I explained here: Imshow subplots with the same colorbar

Categories

Resources