{"id":19296352,"url":"https://github.com/dboyliao/matplotlib_notes","last_synced_at":"2025-10-10T12:33:25.611Z","repository":{"id":19027496,"uuid":"22251477","full_name":"dboyliao/Matplotlib_notes","owner":"dboyliao","description":null,"archived":false,"fork":false,"pushed_at":"2016-02-28T05:53:02.000Z","size":1942,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-05T21:30:15.097Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dboyliao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-25T09:17:31.000Z","updated_at":"2016-02-28T05:53:03.000Z","dependencies_parsed_at":"2022-09-25T03:50:48.475Z","dependency_job_id":null,"html_url":"https://github.com/dboyliao/Matplotlib_notes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dboyliao%2FMatplotlib_notes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dboyliao%2FMatplotlib_notes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dboyliao%2FMatplotlib_notes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dboyliao%2FMatplotlib_notes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dboyliao","download_url":"https://codeload.github.com/dboyliao/Matplotlib_notes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240400293,"owners_count":19795331,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T22:46:23.563Z","updated_at":"2025-10-10T12:33:20.582Z","avatar_url":"https://github.com/dboyliao.png","language":"Jupyter Notebook","readme":"# Notes for Matplolib\n\n# Intro\n\nThere are four privmative drawing objects in `matplotlib`:\n\n- `Line2D`: this object is used to present a 2D line on the graph or any other curves that can be Bezier-approximated.\n- `AxesImage`: this object takes a 2D data and interprete it as densities with a colormap. This is usually returned by the `imshow` method.\n- `Patch`: It represent a 2D object that has a single colored \"face\". This object must have \"path\" which is much like a Line2D object enclosing a a face to filled with that color.\n- `Text`: It represent the text in a graph such as legends, labels,...etc. It takes a string, coordinates and font parameters to instantiate this object.\n\nAny sophisticated plots in matplotlib are build upon these four objects. In the following notes, I will mostly use these four objects to plot all the graph. See the python scripts in `pracs` directory for more advanced examples.\n\nSwitch to `interative` branch to see how to write interative matplotlib applications.\n\n# Events and Callback\n\n## Line Picking\n\n```{python}\nimport matplotlib.pyplot as plt\nfrom matplotlib.collections import LineCollection\nimport numpy as np\nimport pickle\n\nwith open(\"../data/tracks.pickle\") as pf:\n    tracks = pickle.load(pf)\n    tracks = dict((tid, t) for tid, t in tracks.items() if tid != -9)\n\nfig, ax = plt.subplots(1, 1)\n\n# Define th callback function.\n# `event` obj has some useful attributes such as `x`, `y` (the `position`), `xdata` and `ydata` (the `value`).\ndef onpick_callback(event):\n    if not isinstance(event.artist, LineCollection):\n        return\n    lws = event.artist.get_linewidths()\n    print event.ind\n    for i in event.ind:\n        lws[i] = 4 if lws[i] != 4 else 1\n    event.artist.set_linewidths(lws)\n    fig.canvas.draw_idle()\n    # `fig.canvas.draw_idle` is equivalent as `fig.canvas.draw` except the \n    # time when the drawing actually happen. `draw_idle` will wait in a queue\n    # to be queued.\n\nlc = LineCollection(tracks.values(), color = 'b', lw = [1 for _ in range(len(tracks))], picker = True)\nax.add_collection(lc)\nax.autoscale()\n\nfig.canvas.mpl_connect('pick_event', onpick_callback)\nax.set_xlabel('Longitude')\nax.set_ylabel(\"Latitude\")\n\nplt.show()\n```\n\n## Drawing Span\n\n# Interative Applications\n\n# References\n\n- [Matplotlib Doc.](http://matplotlib.org/users/index.html)\n- [Mapplotlib Gallery](http://matplotlib.org/gallery.html)\n- [Challenges](http://www.labri.fr/perso/nrougier/teaching/matplotlib/#d-plots)\n- [Colormaps](http://matplotlib.org/users/colormaps.html)\n- [Transformation](http://matplotlib.org/users/transforms_tutorial.html)\n- [Bezier Curve](https://en.wikipedia.org/wiki/B%C3%A9zier_curve)\n- [Patch Collections](http://matplotlib.org/examples/api/patch_collection.html)\n- [3D subplots](http://matplotlib.org/examples/mplot3d/mixed_subplots_demo.html)\n- [GridSpec](http://matplotlib.org/users/gridspec.html)\n- [fig API](http://matplotlib.org/api/figure_api.html)\n- [Matplotlib Collections](http://matplotlib.org/api/collections_api.html)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdboyliao%2Fmatplotlib_notes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdboyliao%2Fmatplotlib_notes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdboyliao%2Fmatplotlib_notes/lists"}