数据质量分析_绘制直方图和饼状图

本文介绍如何使用Python的Matplotlib库绘制两种基本的数据可视化图表:直方图和饼状图。通过实例展示了设置图表样式、标签及标题的方法,并提供了完整的代码实现。
"""
绘制数据种类直方图、饼状图
"""

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import mlab
from matplotlib import rcParams
import pandas as pd


def draw_zhifangtu():
    fig1 = plt.figure(2)
    rects1 = plt.bar(left=(0.2), height=(0.5), color=('g'), label=(('no1')), width=0.2, align="center", yerr=0.000001)
    rects2 = plt.bar(left=(1), height=(1), color=('r'), label=(('no2')), width=0.2, align="center", yerr=0.000001)
    plt.legend()
    plt.xticks((0.2, 1), ('frst', 'second'))
    plt.title('Pe')

    def autolabel(rects):
        for rect in rects:
            height = rect.get_height()
            plt.text(rect.get_x() + rect.get_width() / 2., 1.03 * height, '%s' % float(height))

    autolabel(rects1)
    autolabel(rects2)
    plt.show()


def draw_bingzhuangtu():
    import numpy as np
    import matplotlib.pyplot as plt

    labels = 'A', 'B', 'C', 'D'
    fracs = [15, 30.55, 44.44, 10]
    explode = [0, 0.1, 0, 0]  # 0.1 凸出这部分,
    plt.axes(aspect=1)  # set this , Figure is round, otherwise it is an ellipse
    # autopct ,show percet
    plt.pie(x=fracs, labels=labels, explode=explode, autopct='%3.1f %%',
            shadow=True, labeldistance=1.1, startangle=90, pctdistance=0.6

            )
    '''
    labeldistance,文本的位置离远点有多远,1.1指1.1倍半径的位置
    autopct,圆里面的文本格式,%3.1f%%表示小数有三位,整数有一位的浮点数
    shadow,饼是否有阴影
    startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看
    pctdistance,百分比的text离圆心的距离
    patches, l_texts, p_texts,为了得到饼图的返回值,p_texts饼图内部文本的,l_texts饼图外label的文本
    '''

    plt.show()


if __name__ == '__main__':
    draw_zhifangtu()
    draw_bingzhuangtu()

转载于:https://my.oschina.net/marjeylee/blog/1516609

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值