Python 数据可视化 气泡图

Python 数据可视化 气泡图

气泡图,说起来高级, 但其实就是散点图添加点的 大小、颜色 参数而已
在这里插入图片描述
fig, ax 分别表示 画布、作图区域

"""
Created on Tue Mar  9 15:43:59 2021

@author: dujidan
"""


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


def noramlization(x):
    x_norm = (x - x.min()) / (x.max() - x.min())
    return x_norm


def drawBubble(x_list, y_list, z_list, x_axis='x_axis', y_axis='y_axis'):
    x = pd.DataFrame(x_list)
    y = pd.DataFrame(y_list)
    z = pd.DataFrame(z_list)
    size = (z-np.min(z)+0.1)*1000
    color = noramlization(z)
    
    # colorbar 颜色大全
    cmap = plt.cm.get_cmap('cool')
    # fig 是图像对象,ax 是坐标轴对象
    fig, ax = plt.subplots()
    bubble = ax.scatter(x, y, s=size, c=color, cmap=cmap, linewidth=0.5, alpha=0.5)
    # 网格线
    ax.grid()
    # 色阶
    fig.colorbar(bubble)
    # X/Y 轴标签
    ax.set_xlabel(f'{x_axis}', fontsize=15)
    ax.set_ylabel(f'{y_axis}', fontsize=15)

    plt.show()


if __name__ == '__main__':
    x = pd.DataFrame([1, 2, 3])
    y = pd.DataFrame([1, 2, 3])
    z = pd.DataFrame([1, 2, 3])

    drawBubble(x, y, z, x_axis='x_axis', y_axis='y_axis')
    

参考:

色阶cmap:http://matplotlib.org/2.0.2/examples/color/colormaps_reference.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值