数据可视化----常用图表样式

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np


# 解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['SimHei']

# 解决负号无法正常显示的问题
plt.rcParams['axes.unicode_minus'] = False

'''
折线图
使用plot()函数
代码格式如下:
plt.plot(x, y, color, linestyle, linewidth, marker, markeredgecolor, markeredgwidth
            markerfacecolor, markersize, label)
    linestyle主要有solid(实线),dashed(虚线),dashdot(线点相接),dotted(虚电线)
    marker:折线图中每点的标记物的形状
    markeredgecolor:标记外边颜色
    markeredgwidth:标记外边线宽
    markerfacecolor:标记物实心颜色
    label:图例
'''

# x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# y = np.array([886, 2335, 5710, 6482, 6120, 1605, 3813, 4428, 4631])
#
# plt.plot(x, y, label='注册用户数量', color='k', linestyle='dashdot',
#          linewidth=1, marker='o', markersize=5)
# plt.title('XXX公司1-9月注册用户数量')
# plt.xlabel('月份')
# plt.ylabel('注册人数')
# plt.grid()
# plt.legend()
# # 添加数据标签
# for a, b in zip(x, y):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
# plt.show()

'''
柱状图
使用bar()函数
代码格式如下:
plt.bar(x, height, width=0.8, bottom=None, align='center', color, edgecolor)
    x:在什么位置显示柱形图
    width:每根柱子宽度可以不同
    bottom:每根柱子的底部位置,可以不同
    aligh:柱子位置与x值得关系,center表示柱子位于x值得中心位置,edge表示柱子位于x值得边缘位置
'''
# 建立一个坐标系
# plt.subplot(1, 1, 1)
# x = np.array(['东区', '北区', '南区', '西区'])
# y = np.array([8566, 6482, 5335, 7310])
# plt.bar(x, y, width=0.5, label='任务量')
# plt.title('全国各区分区任务量')
# plt.xlabel('分区')
# plt.ylabel('任务量')
# for a, b in zip(x, y):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=12)
# plt.legend()
# plt.grid()
# plt.show()

'''
簇状柱形图
'''
# 建立一个坐标系
# plt.subplot(1, 1, 1)
# x = np.array([1, 2, 3, 4])
# y1 = np.array([8566, 6482, 5335, 7310])
# y2 = np.array([4286, 2667, 3655, 3241])
# plt.bar(x, y1, width=0.3, label='任务量')
# plt.bar(x + 0.3, y2, width=0.3, label='完成量')
# plt.title('全国各区分区任务量')
# plt.xlabel('分区')
# plt.ylabel('任务情况')
# for a, b in zip(x, y1):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=12)
# for a, b in zip(x + 0.3, y2):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=12)
# plt.legend()
# plt.grid()
# # 设置x轴刻度值
# plt.xticks(x + 0.15, ['东区', '南区', '西区', '北区'])
# plt.show()

'''
堆积柱状图
'''
# plt.subplot(1, 1, 1)
# x = np.array(['东区', '南区', '西区', '北区'])
# y1 = np.array([8566, 6482, 5335, 7310])
# y2 = np.array([4286, 2667, 3655, 3241])
# plt.bar(x, y1, width=0.5, label='任务量')
# plt.bar(x, y2, width=0.5, label='完成量')
# plt.title('全国各区分区任务量')
# plt.xlabel('分区')
# plt.ylabel('任务情况')
# for a, b in zip(x, y1):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=12)
# for a, b in zip(x, y2):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=12)
# plt.legend()
# plt.grid()
# plt.show()

'''
条形图
使用barh()方法
代码格式如下:
plt.barh(y, width, height, align, color, edgecolor)
    y:在什么位置显示柱子,即纵坐标
    width:横坐标
    height:柱子得宽度
'''
# plt.subplot(1, 1, 1)
# y = np.array([8566, 6482, 6335, 7310])
# x = np.array(['东区', '北区', '南区', '西区'])
# plt.barh(x, height=0.5, width=y, label='任务量')
# plt.xlabel('任务量')
# plt.ylabel('区域')
# plt.legend(loc='upper right')
# plt.grid()
# plt.title('全国各分区任务量')
# for a, b in zip(x, y):
#     plt.text(b, a, b, ha='center', va='center', fontsize=12)
# plt.show()

'''
散点图
散点图常用来发现个变量之间的关系
使用scatter()函数
代码格式如下:
plt.scatter(x, y, s, c, marker, linewidths, edgecolors)
    (x, y):散点位置
    s:散点的大小。如果只有一个值,则每个点的大小一致,可以呈现多个值,让每个点的大小不一致
    c:散点颜色,可以不同
    marker:每个点的标记
'''
# 绘制1-8月平均气温与啤酒销量关系的散点图
# # 气温
# x = np.array([5.5, 6.6, 8.1, 15.8, 19.5, 22.4, 28.3, 28.9])
# # 销量
# y = np.array([2.38, 3.85, 4.41, 5.67, 5.44, 6.03, 8.15, 6.87])
# plt.subplot(1, 1, 1)
# plt.xlabel('平均气温')
# plt.ylabel('啤酒销量')
# plt.title('1-8月平均气温与啤酒销量关系图')
# plt.scatter(x, y, marker='o', s=150)
# for a, b in zip(x, y):
#     plt.text(a, b, b, va='center', ha='center', fontsize=7)
# plt.show()

'''
气泡图
与散点图类似,但图中各点的大小不一致
同样使用scatter()函数
'''
# # 气温
# x = np.array([5.5, 6.6, 8.1, 15.8, 19.5, 22.4, 28.3, 28.9])
# # 销量
# y = np.array([2.38, 3.85, 4.41, 5.67, 5.44, 6.03, 8.15, 6.87])
# colors = y*10  # 根据y值得大小生成不同的颜色
# area = y*100  # 根据y值得大小生成大小不同的形状
# plt.subplot(1, 1, 1)
# plt.xlabel('平均气温')
# plt.ylabel('啤酒销量')
# plt.title('1-8月平均气温与啤酒销量关系图')
# plt.scatter(x, y, marker='o', s=area, c=colors)
# for a, b in zip(x, y):
#     plt.text(a, b, b, va='center', ha='center', fontsize=7, color='white')
# plt.show()

'''
面积图
使用stackplot()函数
代码格式如下:
plt.stackplot(x, y, labels, colors)
    (x, y):x/y坐标数值
    labels:不同系列图表的图例名
'''
# XXX公司1-9月注册与激活人数面积图
# plt.subplot(1, 1, 1)
# x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# y1 = np.array([866, 2335, 5710, 6482, 6120, 1605, 3813, 4428, 4631])
# y2 = np.array([433, 1167, 2855, 3241, 3060,  802, 1906, 2214, 2315])
# labels = ['注册人数', '激活人数']
# # 绘制面积图
# plt.stackplot(x, y1, y2, labels=labels)
# plt.xlabel('月份')
# plt.ylabel('注册与激活人数')
# plt.title('XXX公司1-9月注册与激活人数')
# plt.legend()
# plt.show()

'''
树地图
树地图常用来表示同一等级中不同类别的占比关系
代码格式如下:
squarify.plot(sizes, label, color, value, edgecolor, linewidth)
    sizes:待绘图数据
    value:不同类别的数据标签
'''

# 绘制菊粉星座分布的树地图
# import squarify
#
# # 指定树地图每一块的大小
# size = np.array([3.4, 0.693, 0.585, 0.570, 0.562, 0.531, 0.530, 0.524, 0.501, 0.478, 0.468, 0.436])
#
# # 指定树地图每一块的文字标签
# text = np.array(['未知', '魔羯座', '天枰座', '双鱼座', '天蝎座', '金牛座', '处女座', '双子座', '射手座', '狮子座', '水瓶座', '白羊座'])
#
# # 指定树地图每一块的数值标签
# rate = np.array(['34%', '6.93%', '5.85%', '5.70%', '5.62%', '5.31%',
#                  '5.30%', '5.24%', '5.01%', '4.78%', '4.68%', '4.36%'])
#
# # 指定树地图每一块的颜色
# colors = ['steelblue', '#9999ff', 'red', 'indianred', 'green', 'yellow', 'orange']
#
# # 绘图
# plot = squarify.plot(sizes=size, label=text, color=colors, value=rate, edgecolor='white', linewidth=3)
#
# # 去除坐标轴
# plt.axis('off')
# # 去除上边框和右边框的刻度
# plt.tick_params(top='off', right='off')
# plt.title('菊粉星座分布', fontdict={'fontsize': 12})
# plt.show()

'''
雷达图
雷达图常用来综合评价某一事物,可以直观地看出该事物得优势与不足
使用polar()函数,polar()函数是用来建坐标系的
代码格式如下:
plt.polar(theta, r, color, marker, linewidth)
    theta:每一点在极坐标系中的角度
    r:半径
    marker:每一点的标记物
'''
# XXX数据分析师的综合评级雷达图
# plt.subplot(1, 1, 1, polar=True)  # 参数polar=True表示建立一个极坐标系
# dataLenth = 5  # 将圆分为5份
# # np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)表示在指定的间隔内返回均匀间隔的数字
# # 在[start, stop]范围内计算,返回num个(默认为50)均匀间隔的样本
# # endpoint若为True,则stop为最后一个样本。否则,返回序列不包含stop。默认值为True
# angles = np.linspace(0, 2*np.pi, dataLenth, endpoint=False)
#
# labels = ['沟通能力', '业务理解能力', '逻辑思维能力', '快速学习能力', '工具使用能力']
# data = [2, 3.5, 4, 4.5, 5]
#
# # 闭合各点
# # np.concatenate()函数用于将具有相同结构的array序列结合成一个array
# data = np.concatenate((data, [data[0]]))
# angles = np.concatenate((angles, [angles[0]]))
# plt.polar(angles, data, color='r', marker='o')
# plt.xticks(angles, labels)
#
# plt.title('XXX数据分析师的综合评级')
# plt.show()

'''
箱型图
箱型图用来反映一组数据离散情况
使用boxplot()方法
代码格式如下:
plt.boxplot(x, vert, widths, labels)
    x:待绘图数据
    vert:箱型图方向,True为总像,False为横向,默认为True
'''
# plt.subplot(1, 1, 1)
# y1 = np.array([866, 2335, 5710, 6482, 6120, 1605, 3813, 4428, 4631])
# y2 = np.array([433, 1167, 2855, 3241, 3060, 802, 1906, 2214, 2315])
# x = [y1, y2]
#
# # 绘图
# labels = ['注册人数', '激活人数']
# plt.boxplot(x, labels=labels, widths=[0.2, 0.5])
# plt.title('XXX公司1-9月注册与激活人数')
# plt.show()

'''
饼图
使用pie()方法
代码格式如下:
plt.pie(x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius,
        counterclock, wedgeprops, textprops, center, frame)
        x:待绘图数据
        explods:饼图中每一块离圆心的距离
        labels:饼图中每一块的标签
        autopct:饼图中数值的百分比格式
        pctdistance:数据标签距中心的距离
        shadow:饼图是否有阴影,默认为False
        labeldistance:每一块索引距离中心的距离
        startangle:饼图的初始角度
        counterclock:是否让饼图逆时针显示,默认为True
        wedgeprops:饼图内外边界属性
        textprops:并途中文本相关属性
        center:饼图中心位置
        frame:是否显示饼图背后的图框,默认为False
'''
# plt.subplot(1, 1, 1)
# x = np.array([8566, 5335, 7310, 6482])
# labels = ['东区', '北区', '南区', '西区']
# explode = [0.05, 0, 0, 0]  # 让第一块远离圆心一点
# labeldistance = 1.1
# plt.pie(x, labels=labels, explode=explode, labeldistance=labeldistance, autopct='%.0f%%',
#         radius=1.0)
# plt.title('全国各区域任务量占比')
# plt.show()

'''
圆环图
表示同一层级不同类别的占比关系
使用pie方法,参数与饼图一致,只要在饼图的基础上调整wedgeprops参数即可实现圆环图
'''
# plt.subplot(1, 1, 1)
# # 任务量
# x1 = np.array([8566, 5335, 7310, 6482])
# # 完成量
# x2 = np.array([4283, 2667, 3655, 3241])
# labels = ['东区', '北区', '南区', '西区']
# plt.pie(x1, labels=labels, radius=1.0, wedgeprops=dict(width=0.3, edgecolor='w'))
# plt.pie(x2, radius=0.7, wedgeprops=dict(width=0.3, edgecolor='w'))
#
# # 添加注释
# plt.annotate('完成量', xy=(0.35, 0.35), xytext=(0.7, 0.45),
#              arrowprops=dict(facecolor='black', arrowstyle='->'))
# plt.annotate('任务量', xy=(0.75, 0.20), xytext=(1.1, 0.2),
#              arrowprops=dict(facecolor='black', arrowstyle='->'))
# plt.title('全国各区域任务量占比')
# plt.show()

'''
热力图
将某一事物的响应度反映在图标上,可以快速发现需要重点关注的区域
使用imshow()方法
代码格式如下:
plt.imshow(x, cmap)
    x:待绘图数据,需要时矩阵形式
    cmap:配色方案,表明图标渐变的主题色。cmap的所有可选值都封装在plt.cm中
'''
# import itertools
#
# # 定义一个变量,给变量赋值一个矩阵
# cm = np.array([[1, 0.082, 0.031, -0.0086],
#               [0.082, 1, -0.09, 0.062],
#               [0.031, -0.09, 1, 0.026],
#               [-0.0086, 0.062, 0.026, 1]])
#
# # 设置配色方案
# cmap = plt.cm.cool
# plt.imshow(cm, cmap=cmap)
# # 显示右边颜色条
# plt.colorbar()
#
# # 设置x,y轴坐标刻度值
# classes = ['负债率', '信贷数量', '年龄', '家属数量']
# tick_marks = np.arange(len(classes))
# plt.xticks(tick_marks, classes)
# plt.yticks(tick_marks, classes)
#
# # 将数值显示在指定位置
# # itertools.product(*iterables[, repeat])
# # 笛卡尔积
# # 创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数。
# # horizontalalignment:水平对齐方式
# for i, j in itertools.product(range(cm.shape[0]),
#                               range(cm.shape[1])):
#     plt.text(j, i, cm[i, j], horizontalalignment='center')
# plt.show()

'''
绘制水平线和垂直线
水平线和垂直休闲主要用来做对比参考
使用axhline()和axvline()方法
代码格式如下:
plt.axhline(y, xmin, xmax)
plt.axvline(x, ymin, ymax)
    y/x:画水平/垂直直线时的横/纵坐标
    xmin/xmax:水平线的起点和终点
    ymin/ymax:垂直线的起点和终点
'''
# plt.subplot(1, 2, 1)
# plt.axhline(y=2, xmax=0.6, xmin=0.2)
# plt.subplot(1, 2, 2),
# plt.axvline(x=2, ymax=0.6, ymin=0.2)
# plt.show()

'''
绘图样式设置
matplotlib模块支持你调用其他样式,使用plt.style.available即可查看matplotlib模块支持的所有样式
如果要使用某种样式,只要在程序的开头加上下面这行代码即可
plt.style.use(样式名)
一旦在程序开头指明了使用哪种样式,那么该程序接下来所有图标都会使用这种样式
'''

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值