Python实现数据可视化案例分析

 更新时间:2022年08月03日 15:42:33   作者:biyezuopinvip  
这篇文章主要介绍了Python实现数据可视化案例分析,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下

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 值的函数
  • 将直方图上的数字,位置改到柱形图的内部垂直居中的位置
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 分作为一个分数段,展示出每个分数段的人数直方图。

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 个学期排名数据,并通过直方图进行对比展示。

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")

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

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 波形的频率
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 条曲线。

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()

到此这篇关于Python实现数据可视化案例分析的文章就介绍到这了,更多相关Python数据可视化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用Python的Django框架实现事务交易管理的教程

    使用Python的Django框架实现事务交易管理的教程

    这篇文章主要介绍了使用Python的Django框架实现事务交易管理的教程,针对数据库的事务行为进行一系列操作,要的朋友可以参考下
    2015-04-04
  • Pandas读取Excel文件的六种方法

    Pandas读取Excel文件的六种方法

    Pandas 是一个 Python 数据分析库,可以用来读取 Excel 文件,Pandas提供了多种读取Excel文件的方法,本文将给大家分享一些官方推荐的6种方法,代码示例讲解得非常详细,需要的朋友可以参考下
    2023-11-11
  • python matplotlib饼状图参数及用法解析

    python matplotlib饼状图参数及用法解析

    这篇文章主要介绍了python matplotlib饼状图参数及用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • Python实现繁體转为简体的方法示例

    Python实现繁體转为简体的方法示例

    这篇文章主要介绍了Python实现繁體转为简体的方法,涉及Python编码转换相关操作技巧,需要的朋友可以参考下
    2018-12-12
  • python-yml文件读写与xml文件读写

    python-yml文件读写与xml文件读写

    这篇文章主要介绍了python-yml文件读写与xml文件读写,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • centos6.7安装python2.7.11的具体方法

    centos6.7安装python2.7.11的具体方法

    下面小编就为大家带来一篇centos6.7安装python2.7.11的具体方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • Python 实现淘宝秒杀的示例代码

    Python 实现淘宝秒杀的示例代码

    本篇文章主要介绍了Python 实现淘宝秒杀的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • python中re.findall() 的使用案例

    python中re.findall() 的使用案例

    re.findall() 函数是 python 中正则表达式模块(re)的一个重要函数,它可以根据正则表达式搜索字符串,并返回匹配的字符串列表,这篇文章给大家介绍了python中re.findall() 的使用案例,感兴趣的朋友跟随小编一起看看吧
    2023-09-09
  • 用Python写一个自动木马程序

    用Python写一个自动木马程序

    这篇文章主要介绍了用Python写一个自动木马程序的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • python实现上传下载文件功能

    python实现上传下载文件功能

    这篇文章主要为大家详细介绍了python实现上传下载文件功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07

最新评论