Write a pie chart in Python

Let's write a "pie chart" of people who are hated by everyone in Python. Moreover, write a "double pie chart" (double pie_chart) with gender on the inner pie chart and generations on the outer pie chart. Like this ↓ As usual, the arguments of matplotlib are confusing, so I'll leave them as a note.

pie_chart_#1.png

Pie chart data

For example, suppose you have this kind of JSON data

Input data


data={
      "cluster": 1, 
      "member_cnt": 21343, 
      "generation": {"11-15": 284, "81-85": 216, "0-5": 10, 
                     "76-80": 486, "16-20": 840, "56-60": 1938, 
                     "unknown": 558, "61-65": 1561, "86-90": 51, 
                     "51-55": 2504, "46-50": 2639, "6-10": 18, 
                     "31-35": 1501, "41-45": 2410, "36-40": 1954, 
                     "66-70": 1486, "91 over": 23, "26-30": 989, 
                     "21-25": 976, "71-75": 899},
      "gender": {"M": 11652, "F": 9691}
     }

--cluster: Cluster number (1 in this case) --member_cnt: Number of people in the entire cluster --generation: Age group and the number of people who belong to it are stored in dictionary type (added up to the number of members) --gender: Gender and each number of people are stored in dictionary type (added up to the number of members)

Plot

As a flow of drawing

  1. Create an outer pie chart (generation)
  2. Create an inner pie chart (generation)
  3. Create a white circle on the innermost side It's like that.

Module loading and initialization


# -*- coding: utf-8 -*-
#For jupyter notebook
from IPython.display import display, HTML
%matplotlib inline 

# pie_For chart
import numpy as np
import matplotlib.pyplot as plt

#Specify the colors to be used later (specify the color you like)
c_cycle=("#3498db","#51a62d","#1abc9c","#9b59b6","#f1c40f",
         "#7f8c8d","#34495e","#446cb3","#d24d57","#27ae60",
         "#663399","#f7ca18","#bdc3c7","#2c3e50","#d35400",
         "#9b59b6","#ecf0f1","#ecef57","#9a9a00","#8a6b0e")

Script body


cluster_num = data['cluster']
member_cnt = data['member_cnt']

'''''''''''''''''''''''''''''''''
Create an outer pie chart for generation
'''''''''''''''''''''''''''''''''

generation_labels = list()
generation_values = list()

for gen_cat,cnt in sorted(data['generation'].items(),reverse=True,key=lambda x:x[1]): #Number of members(cnt)Key in descending order of(gen_cat)To sort
    generation_labels.append(gen_cat)
    generation_values.append(cnt)

plt.figure(figsize=(10,8)) #Prepare a window of appropriate size
x = np.array(generation_values) #Np values for pie chart depiction.Assign to array type

plt.pie(x,
       labels=generation_labels, #Specify pie chart label
       colors=c_cycle, #Specify the color of the pie chart (set earlier above)
       wedgeprops={'linewidth': 2,'edgecolor':"white"}, #Add a white border to the pie chart with a thickness of 2
       textprops={'color': "white", 'weight': "bold"}, #Character color and thickness
       pctdistance=0.85, #85 between the center and the circumference of the composition ratio%Output to the position of (fine adjustment of the text display position)
       startangle=90, #Rotate the direction of the circle 90 degrees
       counterclock=False, #Output counterclockwise with false
       autopct=lambda p: '{:.1f}%'.format(p) if p >= 2.5 else '' #2.5%The following components hide numerical values (described later)
      )

plt.axis('equal') #Required to output a pie chart without distortion. If this is not specified, it will be oval.


'''''''''''''''''''''''''''''
Create an inner pie chart for gender
'''''''''''''''''''''''''''''

gender_labels = list()
gender_values = list()

for gender,cnt in data['gender'].items():
    gender_labels.append(gender)
    gender_values.append(cnt)
y = np.array(gender_values)

plt.pie(y,
        labels=gender_labels,
        colors=("#00a2ad","#ffcccc"), # Male,Color assignment for Female
        wedgeprops={'linewidth': 2,'edgecolor':"white"}, #Add a white border with a thickness of 2
        textprops={'color': "#2c3e50", 'weight': "bold"}, #Character color and thickness
        radius=0.7, #70%Draw a circle of the size of. Since gender is an inner pie chart, it is output a little smaller.
        labeldistance=0.8, #Label display position is 80 from the circumference%Display at the position of
        autopct="%1.1f%%", #Output the composition ratio with one decimal place
        pctdistance=0.6, #60 between the center and the circumference of the composition ratio%Output to the position of
        startangle=90, #Rotate the direction of the circle 90 degrees
        counterclock=False #Output counterclockwise with false
       )
plt.axis('equal') #Correct the aspect ratio. Required to output a pie chart without distortion


'''''''''''''''''''''''''''''
Draw a white pie chart on the innermost side (make it look like a donut pie chart)
'''''''''''''''''''''''''''''

center_circle = plt.Circle((0,0),0.4,color='white', fc='white',linewidth=1.25) #center(0,0)To 40%Draw a circle with the size of
fig = plt.gcf()
fig.gca().add_artist(center_circle)

# generation+Gender legend display
lgnd=plt.legend(bbox_to_anchor=(1.0, 0.25, 1.55, 0.5), loc="center left", borderaxespad=0.)

#Displaying the title of the figure
plt.suptitle('cluster#%s (UU=%s)' % (cluster_num,member_cnt),fontsize=25)


#Save pie chart
# bbox~~If there is no such parameter, the legend will protrude from the saved image.
#plt.savefig('./hoge/pie_chart_#{}.png'.format(cluster_num),bbox_exstra_artists=(lgnd),bbox_inches='tight') 
plt.show()

Description

** Make colors fashionable **

-Here was used as a reference. --The default colors of plt.pie make you feel awkward, so here, here, and [here]( I found a nice color by referring to http://colorpalettes.net/) and set it as c_cycle.

** In short, put the value you want to make into a pie chart in np.array and the label in list type **

--For example, in the case of a "generation" pie chart --For labels, store it in a list type with generation_labels.append (gen_cat) --For the value, it corresponds to the place where it is once stored in the list type with generation_values.append (cnt) and then converted to the array type with x = np.array (generation_values). --If you throw those data into plt.pie, it's okay.

** The pie function in matplotlib.pyplot seems to automatically convert the data to percentage values **

--As you can see if you follow the code, if you store the real value read as data in array type (without processing to calculate the ratio) and pass it as an argument of plt.pie, it will be described as a ratio value. It seems ――Then, what if you want to draw a pie chart with the input value as it is without making it a ratio? → I'm sorry, I haven't examined it. .. ..

** Hide elements with a small percentage to improve the appearance **

--The components below \ # 2.5% correspond to the processing of the part where the numerical value is hidden. ――The text of the value overlaps around the element with a small ratio and it looks bad. ――I tried to display the text so that it does not overlap, but I could not find a good method, so I hide the element with a smaller ratio (here, 2.5% or less appropriately)

For other plt.pie parameters, you can understand it by reading the comment out in the code or by actually commenting out the relevant part and seeing how the description changes.

A little sly

When I am writing a pie chart for "generation", if I change "white" in textprops = {'color':" white ",'weight':" bold "} to "black", it actually looks like this. Masu ↓

pie_chart2_#1.png

I'd like to display or delete this overlapping label well, but since it's just character string data, it can't be hidden with conditions like the above "2.5% or less is hidden" ( I couldn't find a good way). If anyone knows a good way to do this, please let me know ... So, for the time being, I made this part white and melted it in the background to cheat.

reference

-Pie chart with [matplotlib (Python)](https://mkacky.wordpress.com/2014/09/28/matplotlib-python-%E3%81%A7%E5%86%86%E3%82%B0% E3% 83% A9% E3% 83% 95 /) -[Draw a pie chart with matplotlib](http://pythondatascience.plavox.info/matplotlib/%E5%86%86%E3%82%B0%E3%83%A9%E3%83%95 /) -Draw a pie chart with matplotlib -Pie chart in Python

Recommended Posts

Write a pie chart in Python
Write a binary search in Python
Write A * (A-star) algorithm in Python
Write a vim plugin in Python
Write a depth-first search in Python
Write the test in a python docstring
Write a short property definition in Python
Write a Caesar cipher program in Python
Write a simple greedy algorithm in Python
Write a simple Vim Plugin in Python 3
Write a nice pie chart with matplotlib
Write Python in MySQL
Take a screenshot in Python
Write Pandoc filters in Python
Create a function in Python
Create a dictionary in Python
Write beta distribution in Python
Write python in Rstudio (reticulate)
Make a bookmarklet in Python
bar chart race in python
Draw a heart in Python
Write a super simple molecular dynamics program in python
I want to write in Python! (2) Let's write a test
Write a log-scale histogram on the x-axis in python
Maybe in a python (original title: Maybe in Python)
How to write a Python class
Write a table-driven test in C
[python] Manage functions in a list
Hit a command in Python (Windows)
Write JSON Schema in Python DSL
Write an HTTP / 2 server in Python
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Create a binary file in Python
Write selenium test code in python
Solve ABC036 A ~ C in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Draw a CNN diagram in Python
Create a random string in Python
Write C unit tests in Python
Schedule a Zoom meeting in Python
Write a batch script with Python3.5 ~
When writing a program in Python
Write documentation in Sphinx with Python Livereload
Generate a first class collection in Python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Solve ABC175 A, B, C in Python
Use print in a Python2 lambda expression
A simple HTTP client implemented in Python
Do a non-recursive Euler Tour in Python
I made a payroll program in Python!
Precautions when pickling a function in python
Display a list of alphabets in Python 3
Try sending a SYN packet in Python
Try drawing a simple animation in Python
Create a simple GUI app in Python
Create a JSON object mapper in Python
Draw a heart in Python Part 2 (SymPy)