Tag Archives: matplotlib

Visual Communication in Python: Pie Charts with Matplotlib

A personal story about how I started using Python for my research: when I was a post-doc at Microsoft, I was embarrassed to ask them to buy me Matlab. But I knew how to plot things in Matlab and I didn’t have time to learn how to make a graphic look nice with Excel or whatever the preferred Microsoft tool was at the time. Matplotlib to the rescue. It was free, it looked *better* than Matlab, and then it was done.

As readers of this blog may know, I have come to use Python extensive in my research by now. But one thing that I have not changed in the 10 years since that post-doc experience is using matplotlib like it was Matlab. It might be time to change.

I recently read a short blog on the modern approach to using Matplotlib, http://pbpython.com/effective-matplotlib.html, and it seems worth a try. Do you remember a talk on data visualization I gave last fall? https://github.com/aflaxman/iths-communicating-results-visually-2

I’m going to try remaking the plots I spoke on with my old school mpl and the modern approach. Here is the first, a pie chart.

My old-fashioned way is in a notebook from my talk, and looks like this:

plt.figure(figsize=(9,8))
plt.subplots_adjust(hspace=.3, right=.8, left=.1)
plt.pie([2,98], labels=['Survived\n2%', 'Died\n98%'], colors=colors, startangle=0)

The new way is built up in this notebook here, and ends up being comparable:

fig, ax = plt.subplots(figsize=(9,8))
s.plot(kind='pie', colors=colors, startangle=0)
fig.subplots_adjust(hspace=.3, right=.8, left=.1)
ax.set_ylabel('')

Is that cooler? I’m not convinced, but I’ll keep trying.

Comments Off on Visual Communication in Python: Pie Charts with Matplotlib

Filed under dataviz

Infographics in Python: Plot a Noun Project Icon on a Matplotlib Chart

I had to put an icon on a chart in Python last week, and I couldn’t find a good brief blog about how to do it. Here is what I cobbled together:

1. Find a free, appropriate image from The Noun Project.
2. Load it into Python with plt.imread
3. Draw it in the proper place on a figure with plt.imshow and some cryptic, hacky options.

Looks good, right?
1500

See this all in action here: https://gist.github.com/aflaxman/c171050384471636e8f23f322ba7e9c5

Comments Off on Infographics in Python: Plot a Noun Project Icon on a Matplotlib Chart

Filed under dataviz