0
点赞
收藏
分享

微信扫一扫

Python数据处理从零开始----第四章(可视化)(20)华夫饼图

目录

Python数据处理从零开始----第四章(可视化)①Matplotlib包
Python数据处理从零开始----第四章(可视化)②图形和轴
Python数据处理从零开始----第四章(可视化)③散点图和误差棒
Python数据处理从零开始----第四章(可视化)④误差图
Python数据处理从零开始----第四章(可视化)⑤(韦恩图)
Python数据处理从零开始----第四章(可视化)⑥(画布设置)
Python数据处理从零开始----第四章(可视化)⑦(多图合并)
Python数据处理从零开始----第四章(可视化)⑧火山图
Python数据处理从零开始----第四章(可视化)⑨线性相关曲线
Python数据处理从零开始----第四章(可视化)⑩ROC曲线

Python数据处理从零开始----第四章(可视化)①①多分类ROC曲线

Python数据处理从零开始----第四章(可视化)①②堆积柱状图

Python数据处理从零开始----第四章(可视化)①③多变量绘图

Python数据处理从零开始----第四章(可视化)(14)使用seaborn绘制热图

Python数据处理从零开始----第四章(可视化)(15)制作签名

Python数据处理从零开始----第四章(可视化)(16)一文解决小提琴图violin plot

Python数据处理从零开始----第四章(可视化)(17)一文解决箱型图box plot

Python数据处理从零开始----第四章(可视化)(18)一文解决柱状图barplot or barchart

Python数据处理从零开始----第四章(可视化)(19)一文解决线图line chart

Python数据处理从零开始----第四章(可视化)(20)华夫饼图

华夫饼图(Waffle Chart),有的人也会叫它“Square Pie Chart”,是饼图的一种变形,擅长展示部分在整体中的占比关系。一般来说,华夫饼图是由100个格子组成,一个格子代表“1%”。用不同颜色的格子区分不同的分类数据,以展示各部分在整体中的占比。华夫饼图(Waffle Chart),或称为直角饼图,可以直观的描绘百分比完成比例情况。与传统的饼图相比较,华夫饼图表达的百分比更清晰和准确,它的每一个格子代表 1%。

# -*- coding: utf-8 -*-
"""
Created on Sat Feb 15 20:33:45 2020

@author: czh
"""

%reset -f
%clear

# In[*]
import matplotlib.pyplot as plt
from pywaffle import Waffle
import os  


os.chdir('D:\\data\\feiyan\\2.15')
# In[*]

fig = plt.figure(
    FigureClass=Waffle, 
    rows=5, 
    columns=10, 
    values=[48, 46, 6],
    figsize=(5, 3)  # figsize is a parameter of matplotlib.pyplot.figure
)
plt.show()


# In[*]

data = {'Democratic': 48, 'Republican': 46, 'Libertarian': 3}
fig = plt.figure(
    FigureClass=Waffle, 
    rows=5, 
    values=data, 
    legend={'loc': 'upper left', 'bbox_to_anchor': (1.1, 1)}
)
plt.show()

# In[*]

data = {'Democratic': 48, 'Republican': 46, 'Libertarian': 3}
fig = plt.figure(
    FigureClass=Waffle, 
    rows=5, 
    values=data, 
    colors=("#983D3D", "#232066", "#DCB732"),
    title={'label': 'Vote Percentage in 2016 US Presidential Election', 'loc': 'left'},
    labels=["{0} ({1}%)".format(k, v) for k, v in data.items()],
    legend={'loc': 'lower left', 'bbox_to_anchor': (0, -0.4), 'ncol': len(data), 'framealpha': 0},
    starting_location='NW'
)
fig.set_facecolor('#EEEEEE')
plt.show()

# In[*]

data = {'Democratic': 48, 'Republican': 46, 'Libertarian': 3}
fig = plt.figure(
    FigureClass=Waffle, 
    rows=5, 
    values=data, 
    colors=("#232066", "#983D3D", "#DCB732"),
    legend={'loc': 'upper left', 'bbox_to_anchor': (1, 1)},
    icons='child', 
    font_size=12, 
    icon_legend=True
)

# In[*]



import pandas as pd
data = pd.DataFrame(
    {
        'labels': ['Hillary Clinton', 'Donald Trump', 'Others'],
        'Virginia': [1981473, 1769443, 233715],
        'Maryland': [1677928, 943169, 160349],
        'West Virginia': [188794, 489371, 36258],
    },
).set_index('labels')

# A glance of the data:
#                  Maryland  Virginia  West Virginia
# labels                                            
# Hillary Clinton   1677928   1981473         188794
# Donald Trump       943169   1769443         489371
# Others             160349    233715          36258


fig = plt.figure(
    FigureClass=Waffle,
    plots={
        '311': {
            'values': data['Virginia'] / 30000,
            'labels': ["{0} ({1})".format(n, v) for n, v in data['Virginia'].items()],
            'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.05, 1), 'fontsize': 8},
            'title': {'label': '2016 Virginia Presidential Election Results', 'loc': 'left'}
        },
        '312': {
            'values': data['Maryland'] / 30000,
            'labels': ["{0} ({1})".format(n, v) for n, v in data['Maryland'].items()],
            'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.2, 1), 'fontsize': 8},
            'title': {'label': '2016 Maryland Presidential Election Results', 'loc': 'left'}
        },
        '313': {
            'values': data['West Virginia'] / 30000,
            'labels': ["{0} ({1})".format(n, v) for n, v in data['West Virginia'].items()],
            'legend': {'loc': 'upper left', 'bbox_to_anchor': (1.3, 1), 'fontsize': 8},
            'title': {'label': '2016 West Virginia Presidential Election Results', 'loc': 'left'}
        },
    },
    rows=5,  # shared parameter among subplots
    colors=("#2196f3", "#ff5252", "#999999"),  # shared parameter among subplots
    figsize=(9, 5)  # figsize is a parameter of plt.figure
)


# In[*]    
    

举报

相关推荐

0 条评论