{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "This example demonstrates many of the 2D plotting capabilities\n",
    "in pyqtgraph. All of the plots may be panned/scaled by dragging with \n",
    "the left/right mouse buttons. Right click on any plot to show a context menu.\n",
    "\"\"\"\n",
    "from pyqtgraph.jupyter import GraphicsLayoutWidget\n",
    "from IPython.display import display\n",
    "\n",
    "import numpy as np\n",
    "import pyqtgraph as pg\n",
    "\n",
    "class CustomGLW(GraphicsLayoutWidget):\n",
    "    def get_frame(self):\n",
    "        # rather than eating up cpu cycles by perpetually updating \"Updating plot\",\n",
    "        # we will only update it opportunistically on a redraw.\n",
    "        # self.request_draw()\n",
    "        update()\n",
    "        return super().get_frame()\n",
    "\n",
    "pg.mkQApp()\n",
    "win = CustomGLW(css_width=\"1000px\", css_height=\"600px\")\n",
    "\n",
    "# Enable antialiasing for prettier plots\n",
    "pg.setConfigOptions(antialias=True)\n",
    "\n",
    "p1 = win.addPlot(title=\"Basic array plotting\", y=np.random.normal(size=100))\n",
    "\n",
    "p2 = win.addPlot(title=\"Multiple curves\")\n",
    "p2.plot(np.random.normal(size=100), pen=(255,0,0), name=\"Red curve\")\n",
    "p2.plot(np.random.normal(size=110)+5, pen=(0,255,0), name=\"Green curve\")\n",
    "p2.plot(np.random.normal(size=120)+10, pen=(0,0,255), name=\"Blue curve\")\n",
    "\n",
    "p3 = win.addPlot(title=\"Drawing with points\")\n",
    "p3.plot(np.random.normal(size=100), pen=(200,200,200), symbolBrush=(255,0,0), symbolPen='w')\n",
    "\n",
    "\n",
    "win.nextRow()\n",
    "\n",
    "p4 = win.addPlot(title=\"Parametric, grid enabled\")\n",
    "x = np.cos(np.linspace(0, 2*np.pi, 1000))\n",
    "y = np.sin(np.linspace(0, 4*np.pi, 1000))\n",
    "p4.plot(x, y)\n",
    "p4.showGrid(x=True, y=True)\n",
    "\n",
    "p5 = win.addPlot(title=\"Scatter plot, axis labels, log scale\")\n",
    "x = np.random.normal(size=1000) * 1e-5\n",
    "y = x*1000 + 0.005 * np.random.normal(size=1000)\n",
    "y -= y.min()-1.0\n",
    "mask = x > 1e-15\n",
    "x = x[mask]\n",
    "y = y[mask]\n",
    "p5.plot(x, y, pen=None, symbol='t', symbolPen=None, symbolSize=10, symbolBrush=(100, 100, 255, 50))\n",
    "p5.setLabel('left', \"Y Axis\", units='A')\n",
    "p5.setLabel('bottom', \"Y Axis\", units='s')\n",
    "p5.setLogMode(x=True, y=False)\n",
    "\n",
    "p6 = win.addPlot(title=\"Updating plot\")\n",
    "curve = p6.plot(pen='y')\n",
    "data = np.random.normal(size=(10,1000))\n",
    "ptr = 0\n",
    "def update():\n",
    "    global curve, data, ptr, p6\n",
    "    curve.setData(data[ptr%10])\n",
    "    if ptr == 0:\n",
    "        p6.enableAutoRange('xy', False)  ## stop auto-scaling after the first data set is plotted\n",
    "    ptr += 1\n",
    "\n",
    "win.nextRow()\n",
    "\n",
    "p7 = win.addPlot(title=\"Filled plot, axis disabled\")\n",
    "y = np.sin(np.linspace(0, 10, 1000)) + np.random.normal(size=1000, scale=0.1)\n",
    "p7.plot(y, fillLevel=-0.3, brush=(50,50,200,100))\n",
    "p7.showAxis('bottom', False)\n",
    "\n",
    "\n",
    "x2 = np.linspace(-100, 100, 1000)\n",
    "data2 = np.sin(x2) / x2\n",
    "p8 = win.addPlot(title=\"Region Selection\")\n",
    "p8.plot(data2, pen=(255,255,255,200))\n",
    "lr = pg.LinearRegionItem([400,700])\n",
    "lr.setZValue(-10)\n",
    "p8.addItem(lr)\n",
    "\n",
    "p9 = win.addPlot(title=\"Zoom on selected region\")\n",
    "p9.plot(data2)\n",
    "def updatePlot():\n",
    "    p9.setXRange(*lr.getRegion(), padding=0)\n",
    "def updateRegion():\n",
    "    lr.setRegion(p9.getViewBox().viewRange()[0])\n",
    "lr.sigRegionChanged.connect(updatePlot)\n",
    "p9.sigXRangeChanged.connect(updateRegion)\n",
    "updatePlot()\n",
    "\n",
    "display(win)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
