
Effectively Using Matplotlib - kercker
http://pbpython.com/effective-matplotlib.html
======
pweissbrod
I 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

~~~
makmanalp
This 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.

------
denfromufa
State of visualization in Python by Jake Vanderplas:

[https://speakerdeck.com/jakevdp/pythons-visualization-
landsc...](https://speakerdeck.com/jakevdp/pythons-visualization-landscape-
pycon-2017)

------
jofer
Another 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/AnatomyOfMatpl...](http://nbviewer.jupyter.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-
Part2-Plotting_Methods_Overview.ipynb)

It gives you a compact visual representation of what the main plotting methods
do and the differences between them.

------
NicoJuicy
I'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
Could 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
[https://startupsventurecapital.com/essential-cheat-sheets-
fo...](https://startupsventurecapital.com/essential-cheat-sheets-for-machine-
learning-and-deep-learning-researchers-efb6a8ebd2e5)

------
Twinklebear
A 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
I'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/)

------
bitL
matplotlib 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
_matplotlib 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
The 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.

------
rjtavares
One 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).

~~~
taeric
I question whether or not animation would really help anything. Rather, I'd
wager it would be like most animated PowerPoints.

~~~
rjtavares
An 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
Like that a lot, is that under-pinned chyronhego data by any chance?

~~~
rjtavares
Thanks! 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-
happ...](https://medium.com/football-crunching/the-zone-where-it-
happens-31320ed89e1f)

[2] - [https://github.com/rjtavares/football-
crunching/blob/master/...](https://github.com/rjtavares/football-
crunching/blob/master/notebooks/working%20with%20positional%20data.ipynb)

------
jbmorgado
I 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).

------
analog31
MPL 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.

------
edshiro
This 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.

------
maxs
I 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.

------
bravura
Biggest 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.

~~~
rogual
I 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.

------
imartin2k
I'm currently learning to use Matplot by visualizing HN activity (very early
stages) so this comes very handy. Thanks for sharing.

~~~
ezequiel-garzon
Interesting! Do you plan to share your findings?

~~~
imartin2k
I 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 :)

------
hyperpallium
How does matplotlib compare with gnuplot?

------
eyeball
Anyone 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
From 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..')`.

------
NelsonMinar
A 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.

------
emilfihlman
Having 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.

------
kronos29296
Very informative. this clears up a lot of doubts I had because I was doing a
lot of snippet copying for my plots before.

------
mynewtb
Everyone, check out toyplot! It is a very easy python module for plotting.

------
j7ake
Are there any advantages of using matplotlib versus say ggplot2?

------
username4444444
from 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
mayavi 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

~~~
guskel
When I was looking into 3d plotting I saw the problems with Mayavi and went
with plotly instead.

~~~
make3
Does plotly have fast 3d?

