基于Python实现数据可视化

1, 问题描述 对右图进行修改 - 请更换图形的风格 - 请将 x 轴的数据改为-10 到 10 - 请自行构造一个 y 值的函数 - 将直方图上的数字

本文包含相关资料包-----> 点击直达获取<-------

1. 问题描述

对右图进行修改 - 请更换图形的风格 - 请将 x 轴的数据改为-10 到 10 - 请自行构造一个 y 值的函数 - 将直方图上的数字,位置改到柱形图的内部垂直居中的位置

  • 对成绩数据 data1402.csv 进行分段统计:每 5 分作为一个分数段,展示出每个分数段的人数直方图。

  • 自行创建出 10 个学生的 3 个学期排名数据,并通过直方图进行对比展示。

  • 线图

  • 把这个图像做一些调整,要求出现 5 个完整的波峰。
  • 调大 cos 波形的幅度
  • 调大 sin 波形的频率

  • 用线图展示北京空气质量数据

展示 10-15 年 PM 指数月平均数据的变化情况,一幅图中有 6 条曲线,每年 1 条曲线。

2. 实验环境

Microsoft Windows 10 版本18363

​ PyCharm 2020.2.1 (Community Edition)

​ Python 3.8(Scrapy 2.4.0 + numpy 1.19.4 + pandas 1.1.4 + matplotlib 3.3.3)

3. 实验步骤及结果

  • 对右图进行修改
  • 请更换图形的风格
  • 请将 x 轴的数据改为-10 到 10
  • 请自行构造一个 y 值的函数
  • 将直方图上的数字,位置改到柱形图的内部垂直居中的位置

```python from matplotlib import pyplot as plt import numpy as np

fig, ax = plt.subplots() plt.style.use('classic') plt.title("square numbers")

ax.set_xlim(-11, 11) ax.set_ylim(0, 100)

x = np.array(range(-10, 11)) y = x * x rect1 = plt.bar(x, y)

for r in rect1: ax.text(r.get_x(), r.get_height() / 2, r.get_height())

plt.show()

```

如图使用 classic 风格,x 轴数据为[-10, 10]的整数,构造的函数为 y=x2,显示位置并将其将数值改到了柱形图内部垂直居中的位置。

  • 对成绩数据 data1402.csv 进行分段统计:每 5 分作为一个分数段,展示出每个分数段的人数直方图。

```python from matplotlib import pyplot as plt import numpy as np import pandas as pd

df = pd.read_csv("./data1402.csv", encoding='utf-8', dtype=str) df = pd.DataFrame(df, columns=['score'], dtype=np.float)

section = np.array(range(0, 105, 5))

result = pd.cut(df['score'], section) count = pd.value_counts(result, sort=False)

fig, ax = plt.subplots() plt.style.use('classic')

ax.set_xlim(0, 100)

rect1 = plt.bar(np.arange(2.5, 100, 5), count, width=5)

for r in rect1: ax.text(r.get_x(), r.get_height(), r.get_height())

plt.show()

```

  • 自行创建出 10 个学生的 3 个学期排名数据,并通过直方图进行对比展示。

```c++ import random

semester1 = np.arange(1, 11) semester2 = np.arange(1, 11) semester3 = np.arange(1, 11)

random.shuffle(semester1) random.shuffle(semester2) random.shuffle(semester3)

df = pd.DataFrame({'semester1':semester1, 'semester2':semester2, 'semester3':semester3}) print(df)

df.to_csv("data1403.csv", encoding="utf-8") ```

使用如上代码创建出随机的排名数据。

```c++ df = pd.read_csv("./data1403.csv", encoding='utf-8', dtype=str) df = pd.DataFrame(df, columns=['semester1', 'semester2', 'semester3'], dtype=np.int)

df['total'] = (df['semester1'] + df['semester2'] + df['semester3']) / 3 df = df.sort_values('total')

fig, ax = plt.subplots() plt.style.use('classic') plt.title('RANK')

width = 0.2 x = np.array(range(0, 10)) rect1 = ax.bar(x-2*width, df['semester1'], width=width, label='semester1') rect2 = ax.bar(x-width, df['semester2'], width=width, label='semester2') rect3 = ax.bar(x, df['semester3'], width=width, label='semester3')

for r in rect1: ax.text(r.get_x(), r.get_height(), r.get_height()) for r in rect2: ax.text(r.get_x(), r.get_height(), r.get_height()) for r in rect3: ax.text(r.get_x(), r.get_height(), r.get_height())

plt.legend(ncol=1) plt.show() ```

如上代码绘图:

  • 线图
  • 把这个图像做一些调整,要求出现 5 个完整的波峰。
  • 调大 cos 波形的幅度
  • 调大 sin 波形的频率

```python import numpy as np from matplotlib import pyplot as plt

x = np.linspace(-5 * np.pi, 5 * np.pi, 500) y1 = 3 * np.cos(x) y2 = np.sin(4*x)

fig, ax = plt.subplots() plt.style.use('classic')

ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.spines['bottom'].set_position(('data',0)) ax.xaxis.set_ticks_position('bottom') ax.spines['left'].set_position(('data',0)) ax.yaxis.set_ticks_position('left')

plt.plot(x, y1, color='blue', linestyle='-', label='y=3cosx') plt.plot(x, y2, color='red', linestyle='-', label='y=sin3x')

plt.legend() plt.show()

```

  • 用线图展示北京空气质量数据

展示 10-15 年 PM 指数月平均数据的变化情况,一幅图中有 6 条曲线,每年 1 条曲线。

```c++ import numpy as np import pandas as pd from matplotlib import pyplot as plt

orig_df = pd.read_csv("./BeijingPM20100101_20151231.csv", encoding='utf-8', dtype=str) orig_df = pd.DataFrame(orig_df, columns=['year', 'month', 'PM_US Post']) df = orig_df.dropna(0, how='any') df['month'] = df['month'].astype(int) df['year'] = df['year'].astype(int) df['PM_US Post'] = df['PM_US Post'].astype(int) df.reset_index(drop=True, inplace=True) num = len(df)

section = np.arange(1, 13) record = 0 fig, ax = plt.subplots() plt.style.use('classic') plt.title("2010-2015 Beijing average PM2.5(from PM_US Post) per month")

for nowyear in range(2010, 2016): i = record result = [0 for i in range(13)] nowsum = 0 cntday = 0 nowmonth = 1 while i < num: if df['month'][i] == nowmonth: cntday = cntday + 1 nowsum = nowsum + df['PM_US Post'][i] else: if df['year'][i] != nowyear: record = i result[nowmonth] = nowsum / cntday break result[nowmonth] = nowsum / cntday cntday = 1 nowsum = df['PM_US Post'][i] nowmonth = df['month'][i] i = i + 1

result = result[1:]

#
x = np.array(range(1, 13))

plt.plot(x, result, linestyle='-', label=str(nowyear))

plt.legend() plt.show()

```

4. 心得与体会

在本次实验中,通过几道题目的引导,逐步掌握了 python 中使用 matplotlib 并结合之前的数据分析处理工具 numpy 及 pandas 等对数据可视化的基本方法。非常感谢老师的实验设计!

参考文献

  • 大数据可视化统计分析通用平台的设计与实现(北京交通大学·张阳)
  • 基于Python的非结构化数据检索系统的设计与实现(南京邮电大学·董海兰)
  • 数据可视化技术在远程教育中的应用研究(西安电子科技大学·齐富)
  • 数据可视化技术及其在疫情防控中的应用研究(武汉轻工大学·蒋迎香)
  • 基于web的统计年鉴可视化系统设计与实现(北京邮电大学·郭梦杰)
  • 基于Vue的数据可视化系统的设计与实现(北京邮电大学·刘翔宇)
  • 基于数据中台系统的可视化设计与实现(华中科技大学·章杰)
  • 电商系统中数据可视化技术研究(东华大学·陈鹏鹏)
  • 面向布局推荐的数据可视化系统设计与实现(西安电子科技大学·施皓)
  • 面向决策支持的数据可视化系统设计与实现(大连理工大学·仲冠霖)
  • 基于.Net平台的数据可视化软件的设计与实现(厦门大学·刘圣晨)
  • 面向决策支持的数据可视化系统设计与实现(大连理工大学·仲冠霖)
  • 基于空间维度的数据可视化系统研建(北京林业大学·孙麒傲)
  • 面向决策支持的数据可视化系统设计与实现(大连理工大学·仲冠霖)
  • 基于数据中台系统的可视化设计与实现(华中科技大学·章杰)

本文内容包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主题。发布者:源码客栈 ,原文地址:https://m.bishedaima.com/yuanma/36158.html

相关推荐

  • 基于SpringBoot的酒店管理系统

    Java组件课程设计-基于SpringBoot的酒店管理系统 1,项目简介 1,1系统角色 系统角色分为:经理(管理员)和员工(普通用户)两种用户
    2024年05月14日
    2 1 1
  • 基于SpringBoot框架的新闻推荐系统

    这是一项利用🔥🔥SpringBoot框架构建的新闻推荐系统工程,采用Java编程语言,结合SpringBoot和Vue,js技术栈进行开发,开发工具为Idea或Eclipse
    2024年05月23日
    2 1 2
  • SSM项目整合

    SSM 整合系列之 SSM 项目整合 Spring,SpringMVC 和 MyBatis 已经是非常成熟的框架了,使用该框架开发已有些时日了,从今天开始呢就对用此框架开发的过程进行一系列的总结
    2024年05月14日
    4 1 1
  • 基于SpringBoot框架的交友网站

    这是一份采用🔥🔥SpringBoot为核心的社交网站开发源码🔥🔥,主要编程语言为Java,并结合了SpringBoot和Vue技术栈,开发工具包括Idea或Eclipse
    2024年05月23日
    2 1 1
  • Java+JavaWeb在线考试系统

    在当今信息化时代,网络教育日益普及,而在线考试系统作为教育信息化的重要组成部分,受到了广泛关注,Java+JavaWeb在线考试系统以其高效,便捷的特点,成为解决传统考试模式中诸多不便之处的有效途径
    2024年05月07日
    4 1 1
  • python实验作业集

    问题集合 1,使用 for 循环,输入倒数秒数,进行倒数计时! 代码如下: ```python import time 使用input()语句接收用户输入的倒计时数值 countdown = int(input("请输入倒数秒数 :")) print("\n倒数计时开始") for i in range(countdown): print("倒数 {} 秒"
    2024年05月14日
    2 1 1
  • 基于SSM实现的客户管理系统

    1 概述 一个简单的客户关系管理系统,管理客户的基本数据,客户的分配,客户的流失以及客户的状态, 2 技术 SSM + Jdk1,8 + MySql5
    2024年05月14日
    3 1 2
  • 论坛网站

    这是一个🔥🔥基于SpringBoot框架的论坛网站设计与实现🔥🔥的项目源码,开发语言Java,框架使用的SpringBoot+vue技术,开发环境Idea/Eclipse
    2024年05月23日
    3 1 1
  • Python爬虫实战之爬取拉勾网职位

    Python爬虫实战之爬取拉勾网职位 1,爬取拉钩招聘动态网页 网址 :https://www,lagou,com/zhaopin/ 通过 分析 网页结构
    2024年05月14日
    2 1 1
  • 仿写 Spring 的一个乞丐版 IOC/AOP 框架

    仿写 Spring 的一个乞丐版 IOC/AOP 框架 讲道理,感觉自己有点菜,Spring 源码看不懂,不想强行解释,等多积累些项目经验之后再看吧,但是 Spring 中的控制反转(IoC)和面向切面编程(AOP)思想很重要
    2024年05月14日
    7 1 3

发表回复

登录后才能评论