[PYTHON] Write a nice pie chart with matplotlib

Abstract

matplotlib This is your own way to draw pie charts neatly. If you write the following data that needs various adjustments in a pie chart, it will be troublesome to adjust each time.

――The label is long or short. --There are 10 or more data, or 1 data.

code

# coding:utf-8

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

###data###
data=[1011,530,355,200,40,11]
label=['hoge','fuga','piyo','pugya','dododododododo','ga']

###Magic to write beautifully###
plt.style.use('ggplot')
plt.rcParams.update({'font.size':15})

###Various parameters###
size=(9,5) #Keep size horizontally long because of the placement of the legend.
col=cm.Spectral(np.arange(len(data))/float(len(data))) #Select the color you like from the color map.

###pie###
plt.figure(figsize=size,dpi=100)
plt.pie(data,colors=col,counterclock=False,startangle=90,autopct=lambda p:'{:.1f}%'.format(p) if p>=5 else '')
plt.subplots_adjust(left=0,right=0.7)
plt.legend(label,fancybox=True,loc='center left',bbox_to_anchor=(0.9,0.5))
plt.axis('equal') 
plt.savefig('figure.png',bbox_inches='tight',pad_inches=0.05)

It looks like this with only plt.pie (data), figure_1.png

I feel like this. figure2.png

Reference site

Colormap is your favorite from the following site. http://matplotlib.org/examples/color/colormaps_reference.html Various options http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.pie

http://d.hatena.ne.jp/a-hisame/20150424/1429875458

Recommended Posts

Write a nice pie chart with matplotlib
Write a pie chart in Python
Write a stacked histogram with matplotlib
A python graphing manual with Matplotlib.
Draw a loose graph with matplotlib
Write a Residual Network with TFLearn
Make a nice graph with plotly
Write a batch script with Python3.5 ~
Make a partially zoomed figure with matplotlib
Write SVG graphs with matplotlib on heroku
Write a TCP client with Python Twisted
Create a graph with borders removed with matplotlib
Draw a flat surface with a matplotlib 3d graph
Animation with matplotlib
Draw a graph with matplotlib from a csv file
Animation with matplotlib
Histogram with matplotlib
Animate with matplotlib
I want to write to a file with Python
Visualize grib2 on a map with python (matplotlib)
[Python] How to draw a line graph with Matplotlib
Forcibly draw something like a flowchart with Python, matplotlib
[Python] A memo to write CSV vertically with Pandas
I want to manually create a legend with matplotlib
A program to write Lattice Hinge with Rhinoceros with Python
[Python, ObsPy] I wrote a beach ball with Matplotlib + ObsPy
[Python] How to create a 2D histogram with Matplotlib
[Python] How to draw a scatter plot with Matplotlib
[Python] Road to a snake charmer (5) Play with Matplotlib
2-axis plot with Matplotlib
A4 size with python-pptx
A memorandum about matplotlib
Heatmap with Python + matplotlib
Band graph with matplotlib
Real-time drawing with matplotlib
Various colorbars with Matplotlib
3D plot with matplotlib
Decorate with a decorator
[Graph drawing] I tried to write a bar graph of multiple series with matplotlib and seaborn
Adjust axes with matplotlib
Study math with Python: Draw a sympy (scipy) graph with matplotlib
Let's create a PRML diagram with Python, Numpy and matplotlib.
[Linux] Write a deployment tool using rsync with a shell script