Skip to content

Plotting

Pyreball currently supports plotting figures created by Matplotlib, Seaborn, Vega-Altair, Plotly, and Bokeh.

There is a single function print_figure() for all these libraries. Similarly to print_table(), it uses parameters caption, align, caption_position and numbered with the same meaning. In contrast to table captions, the default position of figure captions is bottom.

Matplotlib

When plotting with Matplotlib, it is necessary to create a figure object and pass it to print_figure().

In case of Matplotlib, a user can select the format of the figure via matplotlib_format parameter: either "png" or "svg". In case of "svg", it is also possible to choose whether the figure should be embedded into the HTML file or saved into a separate file and referenced in the HTML file by setting embedded accordingly. Figures in "png" format cannot be embedded into the HTML file.

When the figure is stored into a file, the file is saved in a directory with name equal to the filename stem of the HTML file. For example, for HTML file report.html, the image file will be stored in directory report.

The following code shows an example of a bar chart created with Matplotlib and stored in a "png" format.

import matplotlib.pyplot as plt

import pyreball as pb

fig, ax = plt.subplots()
plt.bar([1, 2, 3], [4, 3, 6])
plt.xlabel("x")
plt.ylabel("y")
pb.print_figure(
    fig,
    caption="Matplotlib barchart as png.",
    matplotlib_format="png",
    embedded=False,
)

The next example shows the same chart, but embedded directly into the HTML document in "svg" format.

import matplotlib.pyplot as plt

import pyreball as pb

fig, ax = plt.subplots()
plt.bar([1, 2, 3], [4, 3, 6])
plt.xlabel("x")
plt.ylabel("y")
pb.print_figure(
    fig,
    caption="Matplotlib barchart as svg.",
    matplotlib_format="svg",
    embedded=True,
)

Seaborn

Seaborn is based on Matplotlib and therefore the code is very similar. It is also necessary to create a figure, which is then passed to Pyreball. It is also possible to use parameters matplotlib_format and embedded.

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

import pyreball as pb

df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 3, 6]})

fig, ax = plt.subplots()
sns.barplot(x="x", y="y", data=df, ax=ax, color=sns.xkcd_rgb["windows blue"])
ax.set(xlabel="x", ylabel="y")
pb.print_figure(
    fig,
    caption="Seaborn barchart as png.",
    matplotlib_format="png",
    embedded=False,
)

Vega-Altair

For Vega-Altair charts, Pyreball does not offer any special parameters like for Matplotlib and Seaborn charts. The charts are always embedded into the HTML and kept interactive.

import altair as alt
import pandas as pd

import pyreball as pb

df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 3, 6]})

fig = (
    alt.Chart(df)
    .mark_bar()
    .encode(
        x=alt.X(
            "x",
            type="nominal",
            sort=None,
            title="x",
            axis=alt.Axis(labelAngle=0),
        ),
        y=alt.Y("y", type="quantitative", title="y"),
        tooltip=["x", "y"],
    )
    .properties(
        width=540,
        height=400,
    )
)
pb.print_figure(fig, caption="Vega-Altair barchart.")

The previous example used altair.Chart object, but other types of charts are also supported, e.g. altair.ConcatChart, altair.HConcatChart.

Plotly

Pyreball supports interactive charts created by Plotly, too.

import pandas as pd
import plotly.express as px

import pyreball as pb

df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 3, 6]})

fig = px.bar(df, x="x", y="y")
pb.print_figure(fig, caption="Plotly chart.")

Bokeh

Another library for plotting interactive charts supported by Pyreball is Bokeh.

import pandas as pd
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure

import pyreball as pb

df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 3, 6]})
df["x"] = df["x"].astype(str)
fig = figure(x_range=df["x"])
fig.vbar(x="x", top="y", width=0.9, source=ColumnDataSource(data=df))
fig.add_tools(HoverTool(tooltips=[("x", "@x"), ("y", "@y")]))
pb.print_figure(fig, caption="Bokeh barchart.")