17 ms·
Effectively Using Matplotlib
- edshiro 9y agoThis looks like a great resource! I am currently picking up deep learning and one of the things that they understandably don't cover much is how to use matplotlib. But being able to visualise the problem or your solution is so important to build more intuition and become a better wannabe data scientist.
- rjtavares 9y agoOne aspect of matplotlib that is often overlooked is the animation capabilities. There should be more animations in data-sciency stuff (there's a reason small gifs spread so easilly on the internet).
- asfdsfggtfd 9y agoIt can be used for interactive widgets as well.
- chestervonwinch 9y agoExample: [1]. As part of a python library I wrote for querying and manipulating annotations of a medical image dataset, I added, using matplotlib, a very basic DICOM-viewer that interactively flips through slices of chest CT scans and displays annotation info. [1]: https://raw.githubusercontent.com/pylidc/pylidc/master/img/viz-in-scan-example.png https://raw.githubusercontent.com/pylidc/pylidc/master/img/v...
- randlet 9y agoYou may already be aware of it but dicompyler is a nice dicom viewer built in python and it's quite easy to build plugins for it.
- taeric 9y agoI question whether or not animation would really help anything. Rather, I'd wager it would be like most animated PowerPoints.
- wtallis 9y agoIf you're using the animation to add another axis to the plot, that can be extremely useful. But using it to make annotations zip around the screen would be pathetic.
- lottin 9y agoIt's useful in that it allows visualising an extra dimension, most likely time, but I agree it has great potential for misuse. Also it doesn't work on printed material.
- rjtavares 9y agoAn example of something I made with matplotlib: https://streamable.com/dui9k https://streamable.com/dui9k If there's a time dimension, there should be an animation.
- ysr23 9y agoLike that a lot, is that under-pinned chyronhego data by any chance?
- rjtavares 9y agoThanks! That's actually data I collected myself. This animation is part of a blog post I just published [1]. I also have a notebook on github with an example (data & code - [2]) [1] - https://medium.com/football-crunching/the-zone-where-it-happens-31320ed89e1f https://medium.com/football-crunching/the-zone-where-it-happ... [2] - https://github.com/rjtavares/football-crunching/blob/master/notebooks/working%20with%20positional%20data.ipynb https://github.com/rjtavares/football-crunching/blob/master/...
- trextrex 9y agoWe recently released a thin wrapper around matplotlib that makes it easier to do live plots [1] (since matplotlib has a few gotchas). We use it in a fair number of projects internally, since it makes it easier to monitor performance of various models as they are trained, which shortens the code-test loop. [1] https://github.com/IGITUGraz/live-plotter https://github.com/IGITUGraz/live-plotter
- radarsat1 9y agoAre you just referring to ion() and draw(), or do you mean something more specific to animation? I'm really curious, as I use matplotlib in interactive mode frequently, but I find it too slow for showing real-time updates of some on-going compute process. (It literally slows down my computations.. I thought about coming up with some kind of multiprocess answer to this but it would still have slow updates.) Actually I'd be really interested in a simpler library than matplotlib that is specifically designed for live, parallel updates using super fast GPU-driven drawing methods.
- rjtavares 9y agoI'm talking about the animation module: https://matplotlib.org/2.0.0/api/animation_api.html https://matplotlib.org/2.0.0/api/animation_api.html There's also moviepy, which integrates well with matplotlib (they take RGB arrays as inputs to build the video file, but it has an helper function that converts matplotlib figures into numpy RGB arrays - that should be easy to parallelize). Link: http://zulko.github.io/blog/2014/11/29/data-animations-with-python-and-moviepy/ http://zulko.github.io/blog/2014/11/29/data-animations-with-...
- radarsat1 9y agoAh thanks, I haven't used that.
- MereInterest 9y agoMy current issue with the animation module is that there is no way to clear an animation away. You can clear the figure, but the animation will redraw on top of the cleared figure. The only workaround is to delete the FuncAnimation object, but that is difficult to do in a language without deterministic destruction.
- rjtavares 9y agoHave you tried moviepy? It works by converting matplotlib figures into numpy arrays (you can even pre-build a list of arrays and then just iterate over the list). http://zulko.github.io/blog/2014/11/29/data-animations-with-python-and-moviepy/ http://zulko.github.io/blog/2014/11/29/data-animations-with-...
- NicoJuicy 9y agoI'm picking up reinformencent deep learning and documenting progress with jupyter notebook. Improving my python on the way ( and knowing numpy and matplotlib) has been a great experience the last 2 days. Although the progress seems to be "slow" ( translating formulas, n armed bandits to code ...). My best tip: download cheatsheets for: numpy, pandas, matplotlib, python, ... has been good for getting to know the language and libraries for ML. So this tutorial/information will be put in good hands at a very opportunistic time ;) Thanks!
- raducu 9y agoCould you share those cheatsheets? I'm trying to teach myself ML but I come from a java background so it's really funny writing 30 lines of procedural code only to find out a single line of functional/numpy code would have done the same trick.
- cmav 9y agohttps://startupsventurecapital.com/essential-cheat-sheets-for-machine-learning-and-deep-learning-researchers-efb6a8ebd2e5 https://startupsventurecapital.com/essential-cheat-sheets-fo...
- NicoJuicy 9y agohttp://lmgtfy.com/?q=python+cheatsheet http://lmgtfy.com/?q=python+cheatsheet http://lmgtfy.com/?q=numpy+cheatsheet http://lmgtfy.com/?q=numpy+cheatsheet http://lmgtfy.com/?q=matplotlib+cheatsheet http://lmgtfy.com/?q=matplotlib+cheatsheet ^^
- imartin2k 9y agoI'm currently learning to use Matplot by visualizing HN activity (very early stages) so this comes very handy. Thanks for sharing.
- ezequiel-garzon 9y agoInteresting! Do you plan to share your findings?
- imartin2k 9y agoI have one up on Github. But as I am a Python beginner, I am not sure if this is sophisticated :) Basically, currently I am just accessing the HN API to plot the score graphs for one or multiple story items. https://github.com/martinweigert/hacker_news_analysis https://github.com/martinweigert/hacker_news_analysis So this doesn't really generate any valuable insights. But I'd be happy for suggestions about what type of data/visualization would be valuable. Good to have a challenge to tackle :)
- bitL 9y agomatplotlib is an example of unnecessarily complex and confusing "organic" API. That's why there is so much resentment to use it; trivial things need non-trivial internal understanding and confusing boilerplates.
- gaius 9y agomatplotlib is an example of unnecessarily complex and confusing "organic" API It is designed to be familiar to people who already know MATLAB, and it does that quite well. So it is not "unnecessary", it's like that for a reason. I agree tho' that someone who has never touched MATLAB might want to plot directly from Pandas, or maybe use Seaborn.
- rspeer 9y agoThe problem is that both Pandas and Seaborn are customized by passing their kwargs onto matplotlib, or by giving you back matplotlib axes objects. You have to break through the abstraction pretty much immediately. You can't really put the finishing touches on your graphs without also knowing matplotlib.
- avian 9y agoI didn't have any significant MATLAB background when I started using matplotlib and I found it a joy to use. I think "organic" part of the API is very well integrated and completely optional in most places. For instance, you can use the matlabish shortcut "subplot(111)" or you can spell out the parameters in a pythonic way as "subplot(nrows=1, ncols=1, plot_number=1)".
- omginternets 9y ago>It is designed to be familiar to people who already know MATLAB Yes, and this was a mistake. Other than logical-indexing, there's one is better off forgetting _everything_ about MATLAB.
- kinkrtyavimoodh 9y agoI am fairly comfortable with doing basic matrix work in MATLAB and I am fairly comfortable using Python, but I have found matplotlib to be quite hard to get the hang of. Esp when you add it to pandas which is also slightly idiosyncratic in its own way, and then all of these are different from numpy, which is a mess of its own.
- username4444444 9y agofrom my personal experience, mpl's 3D plotting capabilities are pretty terrible (just try log-scaling your axes) and looking into Mayavi as a replacement has been on the list for a while.
- make3 9y agomayavi is also not amazing (though much better).. afaik there are no good options right now.. If anyone has any suggestions, aside from coding your own thing in PyVTK, I'd really like to know
- dagw 9y agoMayavi is really powerful for 3D plotting and 3D data visualization. Unfortunately the learning curve is quite steep and the documentation is not the best. Also they've built their own python tools for building Mayavi so many things in Mayavi are done in its own, rather unique, way.
- auxym 9y agoI really had my hopes up for VisPy for this (GPU-accelerated 3D plotting). It was supposed to be the convergence of 3 or 4 previously existing OpenGL-based plotting libraries. However, 4 years after starting development, it's still in early stages and not really usable unless you want to code shaders by hand.
- bravura 9y agoBiggest matplotlib frustration: I've spent hours trying to get matplotlib to render on screen on OSX, and followed countless stackoverflow and blog posts instructions. I still can't.
- rhodysurf 9y agoIve had issues on windows before but not osx. Always just installed with pip and it worked.
- AronTrask 9y agoThis may be completely off base but is the issue that you get a run time error telling you that Python is not installed as a framework?
- rogual 9y agoI had this. It turned out that my issue was that "%matplotlib" and "%matplotlib inline" are different, and I was using one when I needed to use the other (I forget which). Edit: Just noticed you didn't mention Jupyter so I guess disregard if you aren't using it.
- Twinklebear 9y agoA cool feature I recently learned about of matplotlib is that it supports LaTeX for text rendering [1]. You can go as far as rendering LaTeX math formatting for titles/labels, or just have the plot fonts match your text and/or figure captions so it fits nicely into your paper. [1] http://matplotlib.org/users/usetex.html http://matplotlib.org/users/usetex.html
- rajasinghe 9y agoI've recently started using this option in gnuplot using the epslatex terminal [1]. Makes for very attractive plots and is relatively simple to use. For those looking for a Matplotlib alternative, I highly recommend it. [1] http://www.gnuplotting.org/output-terminals/ http://www.gnuplotting.org/output-terminals/
- mynewtb 9y agoEveryone, check out toyplot! It is a very easy python module for plotting.
- hyperpallium 9y agoHow does matplotlib compare with gnuplot?
- eyeball 9y agoAnyone know of a good tutorial for plotnine? I'm new to graphing in python and am attracted to this because it should crossover to ggplot2 in R (which I'd also like to learn, but doing python for now). Will ggplot2 tutorials for R be enough to get going with plotnine?
- philh 9y agoFrom what I've seen, pretty much. It seems to be a pretty direct translation (though I've found some bugs that I haven't filed yet). The thing you'll need to do is that when in R you write unquoted expressions in your aes, in plotnine you need to quote them. So `aes(x=foo/3, y=bar, color=..baz..)` becomes `aes(x='foo/3', y='bar', color='..baz..')`.
- pweissbrod 9y agoI needed jupyter as a medium of information sharing in my team but matplotlib has just too much of a learning curve to expect everyone to adopt it as tribal knowledge considering this was not a core part of their job. I found a compromise using the wonderful jupyter_pivottablejs library: https://github.com/nicolaskruchten/jupyter_pivottablejs https://github.com/nicolaskruchten/jupyter_pivottablejs Thus allowing you to tweak visualizations on the fly without touching code. My workflow is: sql -> dataframe -> pivottable This is not a dig at matplotlib which is undeniably powerful. More like an alternative for those of us that want to convey good-enough flexible interactive visualizations without getting into the minutia with matplotlib
- thearn4 9y agoI've commented on this before, but matplotlib is sort of stuck between a rock and a hard place of supporting the cruft of MATLAB plotting syntax, and trying to be pythonic. I'm still a big fan of it because I've grown with it, but I also don't expect that it is the future of python technical plotting. Its more domain-specific, but I do like the seaborn library.
- makmanalp 9y agoThis is brilliant! I always say that matplotlib is more of a low-level charting API - you can do whatever you need, but it'll take a long time and a lot of code. Better is stuff like seaborn, pandas' charting support, and the new ggplot port and altair. Also underutilized is pandas' to_excel, to_clipboard stuff where you can then transfer to whatever application and back for editing / graphing purposes IMHO.
- ocschwar 9y agoThose of us who like matplotlib like it because we got into those minutia with Matlab many years before. If you've not had to endure that hazing ritual, then yes, there are better options.
- kronos29296 9y agoVery informative. this clears up a lot of doubts I had because I was doing a lot of snippet copying for my plots before.
- jofer 9y agoAnother useful guide is Ben Root's Anatomy of Matplotlib tutorial: https://github.com/WeatherGod/AnatomyOfMatplotlib https://github.com/WeatherGod/AnatomyOfMatplotlib I'm a bit biased, as I wrote this particular section (most of the rest is Ben's work), but the plotting method overview is a very useful cheatsheet: http://nbviewer.jupyter.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb http://nbviewer.jupyter.org/github/WeatherGod/AnatomyOfMatpl... It gives you a compact visual representation of what the main plotting methods do and the differences between them.
- jbmorgado 9y agoI want to vouch for Matplotlib, I can see it gets a bad reputation when compared to these new shiny frameworks like plotly, but it's vastly more powerful. If you are a researcher and you want to publish in B&W (something still very common in fields like Physics and Astrophysics), no other plotting library for Python comes near. You can choose filling patterns, line patterns, annotate with LaTeX, etc. And, although hard, you can make your final product look as polished and perfect as you want (and as you are willing to take the time). No other library for Python comes near in these aspects. There are simpler tools and it's easy to get a good enough looking plot, but if you want to get that perfect one exactly as you need, there's no way around Matplotlib (at least amongst the well known Python plotting libraries).
- denfromufa 9y agoState of visualization in Python by Jake Vanderplas: https://speakerdeck.com/jakevdp/pythons-visualization-landscape-pycon-2017 https://speakerdeck.com/jakevdp/pythons-visualization-landsc...
- maxs 9y agoI used matplotlib for a very long time. Now, I suggest using bokeh http://bokeh.pydata.org/ http://bokeh.pydata.org/ I am finding the API a lot cleaner than Matplotlib, and it is very nice to have the ability to do integrated interactive plots in Jupyter.
- emilfihlman 9y agoHaving the graph go beyond a point with the last axis number under it is annoying as hell and everyone who does that should feel bad.
- NelsonMinar 9y agoA fantastic and sorely needed tutorial for orienting matplotlib into modern usage. I really appreciated his description of the matlab-style API vs the object oriented API. Also how to use it with pandas' shortcut methods.
- analog31 9y agoMPL is my go-to graphing tool, but admittedly it's probably because I learned it first and now it's a habit. Almost every Python / Jupyter tutorial starts you out with MPL. But there are two things I like about it: 1. Easy to embed MPL graphics in Tkinter GUI's. Granted, my programs are not intended to be professional looking, but if I want to write stand alone software, e.g., for an automated experiment or industrial test, it invariably needs one or two graphs in a dialog. 2. If what you want is a static graph (no interaction), that's what MPL produces. With other packages that I've tried, every graph is its own JavaScript program running in the browser. A Jupyer notebook with dozens of graphs begins to hog down my computer.
- j7ake 9y agoAre there any advantages of using matplotlib versus say ggplot2?