| 1 |
""" |
| 2 |
.. _chart_basics_example: |
| 3 |
|
| 4 |
Chart Basics |
| 5 |
~~~~~~~~~~~~ |
| 6 |
|
| 7 |
This example shows how different types of charts can be added to the scene. |
| 8 |
A more complex example, showing how to combine multiple charts as overlays |
| 9 |
in the same renderer, is given in :ref:`chart_overlays_example`. |
| 10 |
""" |
| 11 |
|
| 12 |
import numpy as np |
| 13 |
import pyvista as pv |
| 14 |
|
| 15 |
# sphinx_gallery_start_ignore |
| 16 |
# chart background images do not seem to work in interactive mode |
| 17 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 18 |
# sphinx_gallery_end_ignore |
| 19 |
|
| 20 |
rng = np.random.default_rng( |
| 21 |
1 |
| 22 |
) # Seeded random number generator for consistent data generation |
| 23 |
|
| 24 |
# %% |
| 25 |
# This example shows how to create a 2D scatter plot from 100 randomly sampled |
| 26 |
# datapoints using :func:`~pyvista.Chart2D.scatter`. By default, the chart automatically |
| 27 |
# rescales its axes such that all plotted data is visible. By right clicking on the chart |
| 28 |
# you can enable zooming and panning of the chart. |
| 29 |
|
| 30 |
x = rng.standard_normal(100) |
| 31 |
y = rng.standard_normal(100) |
| 32 |
chart = pv.Chart2D() |
| 33 |
chart.scatter(x, y, size=10, style='+') |
| 34 |
chart.show() |
| 35 |
|
| 36 |
# %% |
| 37 |
# To connect datapoints with lines, you can create a 2D line plot as shown in |
| 38 |
# the example below using :func:`~pyvista.Chart2D.line`. You can also dynamically |
| 39 |
# 'zoom in' on the plotted data by specifying a custom axis range yourself. |
| 40 |
|
| 41 |
x = np.linspace(0, 10, 1000) |
| 42 |
y = np.sin(x**2) |
| 43 |
chart = pv.Chart2D() |
| 44 |
chart.line(x, y) |
| 45 |
chart.x_range = [5, 10] # Focus on the second half of the curve |
| 46 |
chart.show() |
| 47 |
|
| 48 |
# %% |
| 49 |
# You can also easily combine scatter and line plots using the general |
| 50 |
# :func:`~pyvista.Chart2D.plot` function, specifying both the line and marker |
| 51 |
# style at once. |
| 52 |
|
| 53 |
x = np.arange(11) |
| 54 |
y = rng.integers(-5, 6, 11) |
| 55 |
chart = pv.Chart2D() |
| 56 |
chart.background_color = (0.5, 0.9, 0.5) # Use custom background color for chart |
| 57 |
chart.plot(x, y, 'x--b') # Marker style 'x', striped line style '--', blue color 'b' |
| 58 |
chart.show() |
| 59 |
|
| 60 |
# %% |
| 61 |
# The following example shows how to create filled areas between two polylines |
| 62 |
# using :func:`~pyvista.Chart2D.area`. |
| 63 |
|
| 64 |
x = np.linspace(0, 10, 1000) |
| 65 |
y1 = np.cos(x) + np.sin(3 * x) |
| 66 |
y2 = 0.1 * (x - 5) |
| 67 |
chart = pv.Chart2D() |
| 68 |
chart.area(x, y1, y2, color=(0.1, 0.1, 0.9, 0.5)) |
| 69 |
chart.line(x, y1, color=(0.9, 0.1, 0.1), width=4, style='--') |
| 70 |
chart.line(x, y2, color=(0.1, 0.9, 0.1), width=4, style='--') |
| 71 |
chart.title = 'Area plot' # Set custom chart title |
| 72 |
chart.show() |
| 73 |
|
| 74 |
# %% |
| 75 |
# Bar charts are also supported using :func:`~pyvista.Chart2D.bar`. |
| 76 |
# Multiple bar plots are placed next to each other. |
| 77 |
|
| 78 |
x = np.arange(1, 13) |
| 79 |
y1 = rng.integers(1e2, 1e4, 12) |
| 80 |
y2 = rng.integers(1e2, 1e4, 12) |
| 81 |
chart = pv.Chart2D() |
| 82 |
chart.bar(x, y1, color='b', label='2020') |
| 83 |
chart.bar(x, y2, color='r', label='2021') |
| 84 |
chart.x_axis.tick_locations = x |
| 85 |
chart.x_axis.tick_labels = [ |
| 86 |
'Jan', |
| 87 |
'Feb', |
| 88 |
'Mar', |
| 89 |
'Apr', |
| 90 |
'May', |
| 91 |
'Jun', |
| 92 |
'Jul', |
| 93 |
'Aug', |
| 94 |
'Sep', |
| 95 |
'Oct', |
| 96 |
'Nov', |
| 97 |
'Dec', |
| 98 |
] |
| 99 |
chart.x_label = 'Month' |
| 100 |
chart.y_axis.tick_labels = '2e' |
| 101 |
chart.y_label = '# incidents' |
| 102 |
chart.show() |
| 103 |
|
| 104 |
# %% |
| 105 |
# In case you want to stack the bars, instead of drawing them next to each |
| 106 |
# other, pass a sequence of y values. |
| 107 |
|
| 108 |
x = np.arange(1, 11) |
| 109 |
ys = [rng.integers(1, 11, 10) for _ in range(5)] |
| 110 |
labels = [f'Machine {i}' for i in range(5)] |
| 111 |
chart = pv.Chart2D() |
| 112 |
chart.bar(x, ys, label=labels) |
| 113 |
chart.x_axis.tick_locations = x |
| 114 |
chart.x_label = 'Configuration' |
| 115 |
chart.y_label = 'Production' |
| 116 |
chart.grid = False # Disable the grid lines |
| 117 |
chart.show() |
| 118 |
|
| 119 |
# %% |
| 120 |
# In a similar way, you can stack multiple area plots on top of |
| 121 |
# each other using :func:`~pyvista.Chart2D.stack`. |
| 122 |
|
| 123 |
x = np.arange(0, 11) |
| 124 |
ys = [rng.integers(1, 11, 11) for _ in range(5)] |
| 125 |
labels = [f'Segment {i}' for i in range(5)] |
| 126 |
chart = pv.Chart2D() |
| 127 |
chart.stack(x, ys, labels=labels) |
| 128 |
chart.show() |
| 129 |
|
| 130 |
# %% |
| 131 |
# Beside the flexible Chart2D used in the previous examples, there are a couple |
| 132 |
# other dedicated charts you can create. The example below shows how a pie |
| 133 |
# chart can be created using :class:`~pyvista.ChartPie`. |
| 134 |
|
| 135 |
data = np.array([8.4, 6.1, 2.7, 2.4, 0.9]) |
| 136 |
chart = pv.ChartPie(data) |
| 137 |
chart.plot.labels = [f'slice {i}' for i in range(len(data))] |
| 138 |
chart.show() |
| 139 |
|
| 140 |
# %% |
| 141 |
# To summarize statistics of datasets, you can easily create a boxplot |
| 142 |
# using :class:`~pyvista.ChartBox`. |
| 143 |
|
| 144 |
data = [rng.poisson(lam, 20) for lam in range(2, 12, 2)] |
| 145 |
chart = pv.ChartBox(data) |
| 146 |
chart.plot.labels = [f'Experiment {i}' for i in range(len(data))] |
| 147 |
chart.show() |
| 148 |
|
| 149 |
# %% |
| 150 |
# If you would like to add other types of chart that are currently not |
| 151 |
# supported by pyvista or VTK, you can resort to matplotlib to create your |
| 152 |
# custom chart and afterwards embed it into a pyvista plotting window. |
| 153 |
# The below example shows how you can do this. |
| 154 |
|
| 155 |
import matplotlib.pyplot as plt |
| 156 |
|
| 157 |
# First, create the matplotlib figure |
| 158 |
f, ax = plt.subplots( |
| 159 |
tight_layout=True, |
| 160 |
) # Tight layout to keep axis labels visible on smaller figures |
| 161 |
alphas = [0.5 + i for i in range(5)] |
| 162 |
betas = [*reversed(alphas)] |
| 163 |
N = int(1e4) |
| 164 |
data = [rng.beta(alpha, beta, N) for alpha, beta in zip(alphas, betas, strict=True)] |
| 165 |
labels = [ |
| 166 |
f'$\\alpha={alpha:.1f}\\,;\\,\\beta={beta:.1f}$' |
| 167 |
for alpha, beta in zip(alphas, betas, strict=True) |
| 168 |
] |
| 169 |
ax.violinplot(data) |
| 170 |
ax.set_xticks(np.arange(1, 1 + len(labels))) |
| 171 |
ax.set_xticklabels(labels) |
| 172 |
ax.set_title('$B(\\alpha, \\beta)$') |
| 173 |
|
| 174 |
# Next, embed the figure into a pyvista plotting window |
| 175 |
pl = pv.Plotter() |
| 176 |
chart = pv.ChartMPL(f) |
| 177 |
chart.background_color = 'w' |
| 178 |
pl.add_chart(chart) |
| 179 |
pl.show() |
| 180 |
# %% |
| 181 |
# .. tags:: plot |