Visualisation backends
Two backends draw the charts, and they do not offer the same ones:
compare and spiral plots and the date slider exist only in bokeh. pyvoa.visualizer.AllVisu routes each request to
the right one according to the vis argument. The javascript the bokeh
backend hands to the browser is documented in Bokeh callbacks.
pyvoa.visu_matplotlib
The matplotlib visualisation backend.
Static charts: the date, versus and yearly plots, the three
histogram kinds, and maps. Every figure is created by the decomatplotlib
decorator, which also stamps the pyvoa logo on it when ‘pyvoalogo’ is True.
Project : pyvoa Authors : Tristan Beau, Julien Browaeys, Olivier Dadoun Copyright ©pyvoa_org License : see the joint LICENSE file https://pyvoa.org/
- class pyvoa.visu_matplotlib.visu_matplotlib[source]
Bases:
objectThe matplotlib backend, drawing static charts.
Offers the ‘date’, ‘versus’ and ‘yearly’ plots, the three histogram kinds and maps. Every figure is created by the
decomatplotlibdecorator, which also stamps the pyvoa logo on it when asked to, so the methods here receive the axes to draw on rather than making them. Each returns those axes, or None when ‘return_pltaxis’ is False.- __init__()[source]
Pick the matplotlib backend that suits the environment.
Chooses the inline backend inside a Jupyter kernel and TkAgg on a plain terminal or an IPython console, falling back to TkAgg if the detection itself fails. Where Tk cannot load, without a display, the backend matplotlib resolved by itself is kept. Doing this at construction keeps the choice out of the drawing methods.
A backend named by the
MPLBACKENDenvironment variable, matplotlib’s own way of asking for one, is left alone. Without that, a script drawing on Agg (to write png files) or the test suite was switched to TkAgg at every chart, and where a display is available each plt.show() then opened a window and waited for it to be closed.
- decomatplotlib()[source]
Decorate creating the figure every matplotlib chart draws on.
Builds a 10x5 figure and its axes, sets the title, and, when ‘pyvoalogo’ is True (it is False by default), stamps the pyvoa logo faintly in the background. The figure, its axes and the pyplot module are passed on as the ‘fig’, ‘ax’ and ‘plt’ keyword arguments, so the drawing methods only have to draw.
- matplotlib_date_plot(**kwargs)[source]
Build the figure, stamp the logo, then call the drawing method.
- matplotlib_versus_plot(**kwargs)[source]
Build the figure, stamp the logo, then call the drawing method.
- matplotlib_yearly_plot(**kwargs)[source]
Build the figure, stamp the logo, then call the drawing method.
pyvoa.visu_bokeh
The bokeh visualisation backend.
The most complete of the three backends: the only one offering the compare
and spiral plots and the date slider, alongside the plots, histograms and
maps the others share. Figures are returned as bokeh objects.
Project : pyvoa Authors : Tristan Beau, Julien Browaeys, Olivier Dadoun Copyright ©pyvoa_org License : see the joint LICENSE file https://pyvoa.org/
- pyvoa.visu_bokeh.safe_output_notebook()[source]
Enable bokeh’s notebook output, but only inside a notebook.
Calling output_notebook() from a plain interpreter emits a warning and achieves nothing, so the environment is detected first and the failure reported through verb() rather than raised.
- class pyvoa.visu_bokeh.visu_bokeh[source]
Bases:
objectThe bokeh backend, drawing interactive charts.
The most complete of the three: it is the only one offering the ‘compare’ and ‘spiral’ plots and the date slider, alongside the plots, histograms and maps the others share. Figures are returned as bokeh objects and shown in the notebook by front.
- __init__()[source]
Set the colour cycles and the default figure size.
Category20 gives the twenty colours locations are cycled through, and Category10 the five used where fewer are needed.
- static rollerJS()[source]
Return the hover formatter shared by the bokeh charts.
Reads pyvoa/js/rollover_callback.js and wraps it in a CustomJSHover, so that the tooltip formatting lives in a javascript file rather than in a string here.
- Returns:
the formatter.
- Return type:
CustomJSHover
- static timestenformatter()[source]
Return the tick formatter of the time series, in ×10 notation.
A tick is written as its mantissa, to one decimal, times a power of ten in superscript (5×10², 1.5×10⁴, or 10³ for a mantissa of 1), and 0 as 0.
- Returns:
the formatter, for the y axis.
- Return type:
CustomJSTickFormatter
- static pyvoalogo(logo)[source]
Read a logo file and return it as a data: URL.
Embedding the image in the document rather than linking it keeps a saved chart self-contained.
- Parameters:
logo (
str) – path to the png.- Returns:
a ‘data:image/png;base64,…’ URL.
- Return type:
- deco_bokeh()[source]
Decorate building the bokeh figure a chart is drawn on.
Gives each location a stable colour from the Category20 cycle, then creates the figure and its title, plus the logo watermark when ‘pyvoalogo’ is True (an empty spacer otherwise), and passes them on to the drawing method.
- static bokeh_legend(bkfigure)[source]
Make a double-click on the figure hide and show the legend.
- Parameters:
bkfigure – the bokeh figure to attach the callback to.
- bokeh_plot()[source]
Decorate dropping the geometry before a non-map chart is drawn.
The non-map charts have no use for the geometry column. The location names need no work here: AllVisu has already cut them to ‘maxlettersdisplayed’.
- bokeh_versus_plot(**kwargs)[source]
Create a versus plot according to arguments.
- Parameters:
input (
pd.DataFrame) – The table the front end assembled: a ‘date’ column, a ‘where’ column, one column per variable ofwhich, and the ‘colors’ columndeco_bokehadds.which (
list) – Exactly two variables, drawn against each other: the first on the x axis, the second on the y axis.title (
str) – The title of the figure.mode (
{'mouse', 'vline', 'hline'}) – How the hover tool follows the pointer.
Notes
The figures themselves and the logo watermark are not arguments a caller provides:
deco_bokehbuilds one figure per axis type and passes them in, under ‘bokeh_figure_linear’ and ‘bokeh_figure_log’.
- bokeh_date_plot(**kwargs)[source]
Create a date plot according to arguments.
- Parameters:
input (
pd.DataFrame) – The table the front end assembled: a ‘date’ column, a ‘where’ column, one column per variable ofwhich, and the ‘colors’ columndeco_bokehadds.which (
list) – The variable(s) to draw against time, one line per variable and per location.title (
str) – The title of the figure.mode (
{'mouse', 'vline', 'hline'}) – How the hover tool follows the pointer.guideline (
bool) – Whether to draw the guide lines.scale (
{'linear', 'log'}) – The axis type the tab of that name carries.
Notes
The figures themselves and the logo watermark are not arguments a caller provides:
deco_bokehbuilds one figure per axis type and passes them in, under ‘bokeh_figure_linear_date’ and ‘bokeh_figure_log_date’.
- bokeh_spiral_plot(**kwargs)[source]
Draw one location’s series as an Archimedean spiral.
Each year is one turn, the day of the year giving the angle, so that seasonality shows up as alignment between successive turns. The value is drawn as a ribbon whose width straddles the baseline. The 29th of February is dropped so that a day number means the same date every year.
- Parameters:
**kwargs – the drawing arguments, including ‘input’ (a single location) and ‘which’.
- Raises:
PyvoaError – if the selection holds more than one location.
Create a date plot with a scrolling menu of locations.
Requires more than two locations.
- Parameters:
input (
pd.DataFrame) – The table the front end assembled: a ‘date’ column, a ‘where’ column, one column per variable ofwhich, and the ‘colors’ columndeco_bokehadds.which (
list) – The variable(s) to draw against time. The menu picks the location.mode (
{'mouse', 'vline', 'hline'}) – How the hover tool follows the pointer.guideline (
bool) – Whether to draw the guide lines.
Notes
The figures themselves and the logo watermark are not arguments a caller provides:
deco_bokehbuilds one figure per axis type, titles them and passes them in, under ‘bokeh_figure_linear_date’ and ‘bokeh_figure_log_date’.
- bokeh_yearly_plot(**kwargs)[source]
Create a yearly plot according to arguments.
Draws every year of the series on the same twelve-month axis, so that the seasons can be compared.
- Parameters:
input (
pd.DataFrame) – The table the front end assembled: a ‘date’ column, a ‘where’ column, one column per variable ofwhich, and the ‘colors’ columndeco_bokehadds.which (
list) – The variable(s) to draw, one curve per year.mode (
{'mouse', 'vline', 'hline'}) – How the hover tool follows the pointer.guideline (
bool) – Whether to draw the guide lines.
Notes
The figures themselves and the logo watermark are not arguments a caller provides:
deco_bokehbuilds one figure per axis type, titles them and passes them in, under ‘bokeh_figure_yearly’ and ‘bokeh_figure_yearly_log’.
- decodateslider()[source]
Decorate adding a date slider to a chart.
Wires a slider to the figure so the drawn date can be moved through the series: maps, histograms by location and pie charts. The chart opens on the last date, with the slider at its end, and a histogram or a pie chart shows the maxcountrydisplayed locations ranked by value at every date, ties by name as
tools.rank_locationsranks them, and a pie chart gathers the other locations, and those undertools.PIE_MINSHAREof the total, into a ‘SumOthers’ slice, as it does without the slider. A map holds one shape per location, which the callback colours with the value of its own location at the date chosen. The default title, which ends on the date, follows the slider, and the colour bar labels its ticks from the range of that date: plainly between 0.001 and 999, over a power of ten otherwise. The histogram by value does not go through this decorator and ignores the slider. Bokeh only; front refuses ‘dateslider’ for other backends.
- value_histo(input, which, bins)[source]
Bin the values of one date for the histogram by value.
- Parameters:
- Returns:
One row per bin: its ‘left’ and ‘right’ edges, ‘middle_bin’, the count as ‘top’, the ‘contributors’ and the bar colour, the same for a bin of a given rank at every date.
- Return type:
pd.DataFrame
- bokeh_histo(**kwargs)[source]
Create a 1D histogram by value according to arguments.
With ‘dateslider’, the histogram of every date is binned beforehand, from the locations
tools.gather_otherskeeps at that date, as without the slider; the chart opens on the last date, and the slider swaps the bins, the axes and the date of the default title.- Parameters:
input (
pd.DataFrame) – The table the front end assembled: a ‘date’ column, a ‘where’ column, one column per variable ofwhich, and the ‘colors’ columndeco_bokehadds. One date, the locations kept and their ‘SumOthers’, without the slider; every date and location with it.which (
str) – The variable whose values are binned. The visualizer has already reduced a list to its single element by this point.bins (
int) – The number of bins. Defaults to the value declared in the keyword catalogue, ten.dateslider (
bool) – Add a slider over the dates.maxcountrydisplayed (
int) – How many locations are binned at a date with the slider, the others making ‘SumOthers’.
Notes
The figures themselves and the logo watermark are not arguments a caller provides:
deco_bokehbuilds one figure per axis type and passes them in, under ‘bokeh_figure_linear’ and ‘bokeh_figure_loglog’.
- bokeh_horizonhisto(**kwargs)[source]
Draw a horizontal bar chart, one bar per location.
Bars are ranked by value and labelled with it. Only maxcountrydisplayed locations are shown at a time, the rest reachable through the widget.
- Parameters:
**kwargs – the drawing arguments, including ‘which’, the column data source, and optionally ‘dateslider’.
- Return type:
The bokeh figure.
- addcolumnshisto(mypd, which, maxcountrydisplayed)[source]
Add the geometry columns a horizontal bar chart needs.
Computes each bar’s left, right, top and bottom edges, and the position and text of its label, so the chart can be drawn from plain rectangles.
- addcolumnspie(df, column_name)[source]
Add the angle columns a pie chart needs.
Turns each value into its share of the total and into the start and end angles of its wedge.
- Parameters:
df (
pd.DataFrame) – one row per location.column_name (
str) – the column holding the value.
- Returns:
df with ‘percentage’, ‘angle’, ‘starts’ and ‘ends’.
- Return type:
pd.DataFrame
- bokeh_pie(**kwargs)[source]
Create a pie chart according to arguments.
- Parameters:
columndatasrc (
bokeh.models.ColumnDataSource) – The slices to draw – one row per location, with its angle, its colour and its share – as the histogram decorator prepared them. This chart is given that source rather than the table itself.which (
str) – The variable whose share each location takes a slice of. The visualizer has already reduced a list to its single element here.mode (
{'mouse', 'vline', 'hline'}) – How the hover tool follows the pointer.dateslider (
bool) – Whether a slider over the dates is drawn beside the chart.controls (
bokeh widgets, optional) – The widgets to lay out beside the chart, the date slider among them.
Notes
The figures themselves and the logo watermark are not arguments a caller provides:
deco_bokehbuilds one figure per axis type and passes them in, under ‘bokeh_figure_linear’.
- bokeh_map(**kwargs)[source]
Draw a choropleth map of one variable.
Colours each location by its value and, unless the map is drawn dense, lays it over the background tiles named by ‘tile’ (‘openstreet’ when none is named), credited in the bottom right corner. The locations the source says nothing about are drawn in pink, and a location without a geometry of its own is counted but not drawn. The pyvoa logo is laid over the figure only when ‘pyvoalogo’ is True, as on the other charts.
The map is framed on every location, pink ones included, as the matplotlib map is. Its plot frame, not counting the colour bar and the notes below it, keeps the proportions of that extent: its short side is 350 px, unless the long side would then pass 800 px, which is then held at 800 px.
- Parameters:
**kwargs – the drawing arguments, including the GeoJSON source, the colour mapper, ‘which’, ‘typeofmap’ and ‘tile’.
- Returns:
The bokeh figure holding the map,in a column under the slider when'dateslider'is asked for, andin a row with the logo when'pyvoalogo'is.
pyvoa.visu_folium
The folium visualisation backend.
Interactive leaflet maps. Only maps are implemented here, folium having no
time-series or histogram equivalent, so AllVisu falls back to another
backend for plots and histograms. Reached through
map(typeofmap='folium').
Project : pyvoa Authors : Tristan Beau, Julien Browaeys, Olivier Dadoun Copyright ©pyvoa_org License : see the joint LICENSE file https://pyvoa.org/
- class pyvoa.visu_folium.visu_folium[source]
Bases:
objectThe folium backend, drawing interactive leaflet maps.
Only maps are implemented here: folium has no time-series or histogram equivalent, which is why AllVisu falls back to another backend for plot() and hist(). Selected through map(typeofmap=’folium’).
- folium_map(**kwargs)[source]
Draw a choropleth map of one variable with folium.
Colours each location by its value using a four-stop viridis-like scale between the smallest and the largest value present, greys out the ones with no value, and attaches a tooltip giving the location and its value in scientific notation. The colour bar is relabelled through a snippet of javascript so that its ticks are shown in scientific notation too.
- Parameters:
**kwargs – the drawing arguments prepared by AllVisu. - input (gpd.GeoDataFrame): the data to draw, carrying a ‘where’ column, a geometry and the column named by ‘what’. - what (str): the column to colour by. - title (str): the caption of the colour bar.
- Returns:
the map, ready to be displayed in a notebook.
- Return type:
folium.Map