Jay's Cookbook
Menu
  • Tags
  • Categories
  • Projects
Computer Science
OS
Network
Data Structure
Algorithm
Language
Code Architecture
Python
Javascript
Typescript
Java
Backend
Backend Theory
TypeORM
Node.js
NestJS
FastAPI
Frontend
HTML/CSS
React
Next.js
Data Engineering
DE Theory
MySQL
MongoDB
Elastic
Redis
Kafka
Spark
Airflow
AI
Basic
Pytorch
NLP
Computer Vision
Data Analytics
Statistics
Pandas
Matplotlib
DevOps
Git
Docker
Kubernetes
AWS
Matplotlib Series [Part9] Pie Plot
data_analytics
matplotlib

Matplotlib Series [Part9] Pie Plot

Jay Kim
Jay Kim 20 Jan 2022
Matplotlib Series [Part8] Histogram Plot Matplotlib Series [Part10] Scatter Plot

Table of Contents

  • 파이 그래프 기본
  • 퍼센티지 표기
  • 파이 정렬
  • 도넛 그래프

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

n_can = 5
votes = np.random.randint(100, 1000, (n_can, ))

cans = np.array(['A', 'B', 'C', 'D', 'E'])

total_vote = np.sum(votes)
percentages = votes/total_vote*100

파이 그래프 기본

fig, ax = plt.subplots(figsize=(3, 3), facecolor='lightsteelblue')

ax.pie(percentages)

퍼센티지 표기

fig, ax = plt.subplots(figsize=(3, 3), facecolor='lightsteelblue')

ax.pie(percentages, autopct='%1.1f%%', textprops={'fontsize': 8, 'color': 'w'})

파이 정렬

sorted_idx = np.argsort(percentages)[::-1]
sorted_percentages = percentages[sorted_idx]
sorted_legend = np.array(cans)[sorted_idx]

fig, ax = plt.subplots(figsize=(5, 5), facecolor='lightsteelblue')

ax.pie(sorted_percentages, counterclock=False, startangle=-270)
ax.legend(sorted_legend)

도넛 그래프

wedgeprops = {'width': 0.6, 'edgecolor': 'skyblue', 'linewidth': 1}

fig, ax = plt.subplots(figsize=(5, 5), facecolor='lightsteelblue')

ax.pie(sorted_percentages, counterclock=False, startangle=-270, autopct='%1.1f%%', wedgeprops=wedgeprops, pctdistance=0.8)
ax.legend(sorted_legend)

cmap = cm.get_cmap(name='Set2', lut=20) 
outer_colors = [cmap(4*i) for i in range(5)]


wedgeprops = {'width': 0.6, 'edgecolor': 'skyblue', 'linewidth': 1}

fig, ax = plt.subplots(figsize=(5, 5), facecolor='lightsteelblue')

ax.pie(sorted_percentages, counterclock=False, startangle=-270, autopct='%1.1f%%', \
       wedgeprops=wedgeprops, pctdistance=0.8, colors=outer_colors)
ax.legend(sorted_legend)

Matplotlib Series [Part8] Histogram Plot Matplotlib Series [Part10] Scatter Plot

You may also like

See all matplotlib
21 Jan 2022 Matplotlib Series [Part11] Box and Whisker Plot
data_analytics
matplotlib

Matplotlib Series [Part11] Box and Whisker Plot

21 Jan 2022 Matplotlib Series [Part10] Scatter Plot
data_analytics
matplotlib

Matplotlib Series [Part10] Scatter Plot

20 Jan 2022 Matplotlib Series [Part8] Histogram Plot
data_analytics
matplotlib

Matplotlib Series [Part8] Histogram Plot

Jay Kim

Jay Kim

Web development, data engineering for human for the Earth. I share posts, free resources and inspiration.

Rest
Lifestyle
Hobby
Hobby
Hobby
Hobby
2025 © Jay's Cookbook. Crafted & Designed by Artem Sheludko.