簡體   English   中英

餡餅甜甜圈並排,使用 matplotlib

[英]Pie Donuts side-by-side, using matplotlib

import matplotlib.pyplot as plt

# First tri 2020 
# Data
months = ['January\n35.97%', 'February\n33.40%', 'March\n30.55%']
months_sizes = [35.97, 33.40, 30.55]
months_colors = ['#081a28', '#2e1d0b', '#15210f']

weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
weekdays_sizes = [5.71, 6.65, 6.66, 7.24, 6.13, 1.65, 1.93, 6.04, 6.16, 5.06, 4.29, 4.72, 3.73, 3.48, 4.61, 5.61, 5.18, 4.66, 3.82, 2.57, 4.09]
weekdays_colors = ['#2986cc', '#2478b7', '#206ba3', '#1c5d8e', '#18507a', '#144366', '#103551',
                   '#e69138', '#cf8232', '#b8742c', '#a16527', '#8a5721', '#73481c', '#5c3a16',
                   '#6aa84f', '#5f9747', '#54863f', '#4a7537', '#3f642f', '#355427', '#2a431f']

# Second tri 2019
# Data
months_2 = ['April\n25.64%', 'May\n33.68%', 'June\n40.68%']
months_sizes_2 = [25.64, 33.68, 40.68]
months_colors_2 = ['#081a28', '#2e1d0b', '#15210f']

weekdays_2 = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
weekdays_sizes_2 = [4.93, 5.23, 4.22, 3.51, 3.74, 2.11, 1.88, 4.17, 4.46, 6.81, 6.59, 5.85, 2.94, 2.87, 6.09, 6.75, 6.08, 6.16, 6.37, 5.07, 4.17]
weekdays_colors_2 = ['#2986cc', '#2478b7', '#206ba3', '#1c5d8e', '#18507a', '#144366', '#103551',
                   '#e69138', '#cf8232', '#b8742c', '#a16527', '#8a5721', '#73481c', '#5c3a16',
                   '#6aa84f', '#5f9747', '#54863f', '#4a7537', '#3f642f', '#355427', '#2a431f']

# Create figure
fig, (ax, ax_2) = plt.subplots(1, 2, figsize=(50,12))

# Plot 1
outer_circle = plt.pie(months_sizes, labels=months, colors=months_colors, radius=1.1, startangle=90, frame=True, textprops={'color':"black", 'size': 11},labeldistance=1.05)
inner_circle = plt.pie(weekdays_sizes, labels=weekdays, colors=weekdays_colors, radius=0.8, startangle=90, textprops={'color':"w", 'size': 10})
centre_circle = plt.Circle((0, 0), 0.35, color='white', linewidth=0)
ax.add_artist(centre_circle)
plt.title('First Trimester (2020)\nVisual Representation of number of rides per weekday per month', loc='center', fontsize=15, color='black')
plt.show()

# Plot 2
outer_circle_2 = plt.pie(months_sizes_2, labels=months_2, colors=months_colors_2, radius=1.1, startangle=90, frame=True, textprops={'color':"black", 'size': 11},labeldistance=1.05)
inner_circle_2 = plt.pie(weekdays_sizes_2, labels=weekdays_2, colors=weekdays_colors_2, radius=0.8, startangle=90, textprops={'color':"w", 'size': 10})
centre_circle_2 = plt.Circle((0, 0), 0.35, color='white', linewidth=0)
ax_2.add_artist(centre_circle_2)
plt.title('Second Trimester (2019)\nVisual Representation of number of rides per weekday per month', loc='center', fontsize=15, color='black')
plt.show()

我試圖並排想象這兩個甜甜圈,但我無法這樣做。 我可以 plot 它們,通常,一個接一個,但是當試圖將它們一起顯示時,結果如下: img 我的白色中心圓圈也消失了。 我究竟做錯了什么?

根據此文檔,您需要將餅圖和其他信息( 例如標題)添加到各個軸:

# -*- coding: utf-8 -*-

# Plot 1
outer_circle = ax.pie(months_sizes, labels=months, colors=months_colors, radius=1.1, startangle=90, frame=True, textprops={'color':"black", 'size': 11},labeldistance=1.05)
inner_circle = ax.pie(weekdays_sizes, labels=weekdays, colors=weekdays_colors, radius=0.8, startangle=90, textprops={'color':"w", 'size': 10})
centre_circle = plt.Circle((0, 0), 0.35, color='white', linewidth=0)
ax.add_artist(centre_circle)
ax.set_title('First Trimester (2020)\nVisual Representation of number of rides per weekday per month', loc='center', fontsize=15, color='black')

# Plot 2
outer_circle_2 = ax_2.pie(months_sizes_2, labels=months_2, colors=months_colors_2, radius=1.1, startangle=90, frame=True, textprops={'color':"black", 'size': 11},labeldistance=1.05)
inner_circle_2 = ax_2.pie(weekdays_sizes_2, labels=weekdays_2, colors=weekdays_colors_2, radius=0.8, startangle=90, textprops={'color':"w", 'size': 10})
centre_circle_2 = plt.Circle((0, 0), 0.35, color='white', linewidth=0)
ax_2.add_artist(centre_circle_2)
ax_2.set_title('Second Trimester (2019)\nVisual Representation of number of rides per weekday per month', loc='center', fontsize=15, color='black')
plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM