python数据可视化(2)——绘制直方图

课程学习来源:b站up:【蚂蚁学python】
【课程链接:【【数据可视化】Python数据图表可视化入门到实战】
【课程资料链接:【链接】】

本节代码

1.读取数据

import pandas as pd
import numpy as np

df = pd.read_excel("../DATA_POOL/PY_DATA/ant-learn-visualization-master/datas/boston-house-prices/housing.xlsx")

2.使用matplotlib画直方图

matplotlib直方图文档链接

import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(12,5))#图片的长宽
plt.hist(df["MEDV"],bins=100)#bins分成100份
plt.show()

效果:

在这里插入图片描述

3.使用pyecharts绘制直方图

pyecharts直方图文档

numpy直方图文档

from pyecharts import options as opts
from pyecharts.charts import Bar

# 自己计算多少个间隔,计算每个间隔有多少值
hist,bin_edges = np.histogram(df["MEDV"],bins=100)

# pyecharts的直方图使用bar实现
# 取bins[:-1],意思是用每个区间的左边元素作为x轴的值
bar = (
    Bar()
    .add_xaxis([str(x) for x in bin_edges[:-1]])
    .add_yaxis("价格分布",[float(x) for x in hist], category_gap=0)
    .set_global_opts(
        title_opts = opts.TitleOpts(title="波士顿房价-价格分布-直方图",pos_left="center"),
        legend_opts = opts.LegendOpts(is_show=False)
    )
)

from IPython.display import HTML

# 同上,读取 HTML 文件内容
# bar.render()的值是一个路径,以字符串形式表示
with open(bar.render(), 'r', encoding='utf-8') as file:
    html_content = file.read()

# 直接在 JupyterLab 中渲染 HTML
HTML(html_content)

效果:
在这里插入图片描述

本节作业

小作业:

  1. 获取你们产品的销量数据、价格数据,提取得到一个数组,画一个直方图看一下数据分布
  2. 获取北京天气数据,取年度365个温度数字,绘制直方图查看温度分布

作业1:大型商超数据集

数据集来源:【https://www.heywhale.com/mw/dataset/656069b19a74cc18269207c4】
在这里插入图片描述

from pyecharts import options as opts
from pyecharts.charts import Bar

hist1,bin_edges_sales = np.histogram(df["Sales"],bins=100000)

bar = (
    Bar()
    .add_xaxis(str(x) for x in bin_edges_sales[:-1])
    .add_yaxis("销量分布",[float(x) for x in hist1], category_gap=1)
    .set_global_opts(
        title_opts = opts.TitleOpts(title="大型商场-销量分布-直方图",pos_left="center"),
        legend_opts = opts.LegendOpts(is_show=False)
    )
)
from IPython.display import HTML

# 同上,读取 HTML 文件内容
# bar.render()的值是一个路径,以字符串形式表示
with open(bar.render(), 'r', encoding='utf-8') as file:
    html_content = file.read()

# 直接在 JupyterLab 中渲染 HTML
HTML(html_content)

在这里插入图片描述
在这里插入图片描述

作业二:北京天气数据集

在这里插入图片描述
在这里插入图片描述
yWendu列有℃符号,导致整列都是object类型,需要先把℃符号去掉,然后转换成int类型

numeric_df = df["yWendu"].str.replace('℃', '').astype(int) 
numeric_df

在这里插入图片描述
然后开始绘图

# 自己计算多少个间隔,计算每个间隔有多少值
hist,bin_edges = np.histogram(numeric_df,bins=365)

# pyecharts的直方图使用bar实现
# 取bins[:-1],意思是用每个区间的左边元素作为x轴的值
bar = (
    Bar()
    .add_xaxis([str(x) for x in bin_edges[:-1]])
    .add_yaxis("温度分布",[float(x) for x in hist], category_gap=0)
    .set_global_opts(
        title_opts = opts.TitleOpts(title="北京天气-温度分布-直方图",pos_left="center"),
        legend_opts = opts.LegendOpts(is_show=False)
    )
)

from IPython.display import HTML

# 同上,读取 HTML 文件内容
# bar.render()的值是一个路径,以字符串形式表示
with open(bar.render(), 'r', encoding='utf-8') as file:
    html_content = file.read()

# 直接在 JupyterLab 中渲染 HTML
HTML(html_content)

在这里插入图片描述

相关推荐

  1. Python数据绘制折线图

    2024-07-12 17:18:02       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 17:18:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 17:18:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 17:18:02       58 阅读
  4. Python语言-面向对象

    2024-07-12 17:18:02       69 阅读

热门阅读

  1. 软设之享元模式

    2024-07-12 17:18:02       20 阅读
  2. 3179. K 秒后第 N 个元素的值

    2024-07-12 17:18:02       22 阅读
  3. mysql中的二进制数据类型

    2024-07-12 17:18:02       20 阅读
  4. mysql8遇到的报错Public Key Retrieval is not allowed

    2024-07-12 17:18:02       22 阅读
  5. MySQL事务

    2024-07-12 17:18:02       20 阅读
  6. C语言阶乘(只用逻辑运算中的短路效应判断)

    2024-07-12 17:18:02       21 阅读
  7. numpy 解释函数nanmax

    2024-07-12 17:18:02       22 阅读
  8. AIGC:AI创作短片-流程以及工具介绍(学习笔记)

    2024-07-12 17:18:02       23 阅读
  9. NLP简介

    NLP简介

    2024-07-12 17:18:02      22 阅读
  10. Linux 内核中的 Makefile 和 Kconfig:深入理解与实践

    2024-07-12 17:18:02       19 阅读
  11. 【Cesium开发实战】淹没分析功能的实现

    2024-07-12 17:18:02       19 阅读