{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Chart Basics {#chart_basics_example}\n\nThis example shows how different types of charts can be added to the\nscene. A more complex example, showing how to combine multiple charts as\noverlays in the same renderer, is given in\n`chart_overlays_example`{.interpreted-text role=\"ref\"}.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nimport pyvista as pv\n\n\nrng = np.random.default_rng(1)  # Seeded random number generator for consistent data generation"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This example shows how to create a 2D scatter plot from 100 randomly\nsampled datapoints. By default, the chart automatically rescales its\naxes such that all plotted data is visible. By right clicking on the\nchart you can enable zooming and panning of the chart.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = rng.standard_normal(100)\ny = rng.standard_normal(100)\nchart = pv.Chart2D()\nchart.scatter(x, y, size=10, style=\"+\")\nchart.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To connect datapoints with lines, you can create a 2D line plot as shown\nin the example below. You can also dynamically \\'zoom in\\' on the\nplotted data by specifying a custom axis range yourself.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.linspace(0, 10, 1000)\ny = np.sin(x**2)\nchart = pv.Chart2D()\nchart.line(x, y)\nchart.x_range = [5, 10]  # Focus on the second half of the curve\nchart.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can also easily combine scatter and line plots using the general\n`pyvista.Chart2D.plot`{.interpreted-text role=\"func\"} function,\nspecifying both the line and marker style at once.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.arange(11)\ny = rng.integers(-5, 6, 11)\nchart = pv.Chart2D()\nchart.background_color = (0.5, 0.9, 0.5)  # Use custom background color for chart\nchart.plot(x, y, 'x--b')  # Marker style 'x', striped line style '--', blue color 'b'\nchart.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following example shows how to create filled areas between two\npolylines.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.linspace(0, 10, 1000)\ny1 = np.cos(x) + np.sin(3 * x)\ny2 = 0.1 * (x - 5)\nchart = pv.Chart2D()\nchart.area(x, y1, y2, color=(0.1, 0.1, 0.9, 0.5))\nchart.line(x, y1, color=(0.9, 0.1, 0.1), width=4, style=\"--\")\nchart.line(x, y2, color=(0.1, 0.9, 0.1), width=4, style=\"--\")\nchart.title = \"Area plot\"  # Set custom chart title\nchart.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Bar charts are also supported. Multiple bar plots are placed next to\neach other.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.arange(1, 13)\ny1 = rng.integers(1e2, 1e4, 12)\ny2 = rng.integers(1e2, 1e4, 12)\nchart = pv.Chart2D()\nchart.bar(x, y1, color=\"b\", label=\"2020\")\nchart.bar(x, y2, color=\"r\", label=\"2021\")\nchart.x_axis.tick_locations = x\nchart.x_axis.tick_labels = [\n    \"Jan\",\n    \"Feb\",\n    \"Mar\",\n    \"Apr\",\n    \"May\",\n    \"Jun\",\n    \"Jul\",\n    \"Aug\",\n    \"Sep\",\n    \"Oct\",\n    \"Nov\",\n    \"Dec\",\n]\nchart.x_label = \"Month\"\nchart.y_axis.tick_labels = \"2e\"\nchart.y_label = \"# incidents\"\nchart.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In case you want to stack the bars, instead of drawing them next to each\nother, pass a sequence of y values.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.arange(1, 11)\nys = [rng.integers(1, 11, 10) for _ in range(5)]\nlabels = [f\"Machine {i}\" for i in range(5)]\nchart = pv.Chart2D()\nchart.bar(x, ys, label=labels)\nchart.x_axis.tick_locations = x\nchart.x_label = \"Configuration\"\nchart.y_label = \"Production\"\nchart.grid = False  # Disable the grid lines\nchart.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In a similar way, you can stack multiple area plots on top of each\nother.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.arange(0, 11)\nys = [rng.integers(1, 11, 11) for _ in range(5)]\nlabels = [f\"Segment {i}\" for i in range(5)]\nchart = pv.Chart2D()\nchart.stack(x, ys, labels=labels)\nchart.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Beside the flexible Chart2D used in the previous examples, there are a\ncouple other dedicated charts you can create. The example below shows\nhow a pie chart can be created.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data = np.array([8.4, 6.1, 2.7, 2.4, 0.9])\nchart = pv.ChartPie(data)\nchart.plot.labels = [f\"slice {i}\" for i in range(len(data))]\nchart.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To summarize statistics of datasets, you can easily create a boxplot.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data = [rng.poisson(lam, 20) for lam in range(2, 12, 2)]\nchart = pv.ChartBox(data)\nchart.plot.labels = [f\"Experiment {i}\" for i in range(len(data))]\nchart.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "If you would like to add other types of chart that are currently not\nsupported by pyvista or VTK, you can resort to matplotlib to create your\ncustom chart and afterwards embed it into a pyvista plotting window. The\nbelow example shows how you can do this.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\n# First, create the matplotlib figure\nf, ax = plt.subplots(\n    tight_layout=True,\n)  # Tight layout to keep axis labels visible on smaller figures\nalphas = [0.5 + i for i in range(5)]\nbetas = [*reversed(alphas)]\nN = int(1e4)\ndata = [rng.beta(alpha, beta, N) for alpha, beta in zip(alphas, betas)]\nlabels = [f\"$\\\\alpha={alpha:.1f}\\\\,;\\\\,\\\\beta={beta:.1f}$\" for alpha, beta in zip(alphas, betas)]\nax.violinplot(data)\nax.set_xticks(np.arange(1, 1 + len(labels)))\nax.set_xticklabels(labels)\nax.set_title(\"$B(\\\\alpha, \\\\beta)$\")\n\n# Next, embed the figure into a pyvista plotting window\np = pv.Plotter()\nchart = pv.ChartMPL(f)\nchart.background_color = 'w'\np.add_chart(chart)\np.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}