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

作者:biyezuopinvip 时间:2022-09-07 17:58:38 

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

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

如图使用 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()

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

自行创建出 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")

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

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

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

如上代码绘图:

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

线图 :

  • 把这个图像做一些调整,要求出现 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()

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

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

展示 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实现数据可视化案例分析

来源:https://blog.csdn.net/newlw/article/details/126127393

标签:Python,数据,可视化
0
投稿

猜你喜欢

  • django template实现定义临时变量,自定义赋值、自增实例

    2021-05-24 09:16:46
  • 将python代码和注释分离的方法

    2022-04-06 12:04:50
  • python实现Nao机器人的单目测距

    2021-04-09 16:37:10
  • 详解Python OpenCV图像分割算法的实现

    2022-11-28 13:15:44
  • ASP+SQL Server构建网页防火墙

    2009-01-21 19:56:00
  • Go 字符串格式化的实例代码详解

    2023-08-05 14:05:30
  • python实现点对点聊天程序

    2023-10-27 11:42:02
  • python调用webservice接口的实现

    2022-10-20 12:54:26
  • ASP程序代码执行时间统计类

    2007-10-15 12:45:00
  • python实现无边框进度条的实例代码

    2023-07-23 05:22:59
  • python的类class定义及其初始化方式

    2023-08-07 11:52:15
  • python 计算一个字符串中所有数字的和实例

    2022-07-19 01:42:33
  • Python requests用法和django后台处理详解

    2023-06-12 02:05:42
  • PHP实现生成Excel文件并导出的示例详解

    2023-05-28 13:11:18
  • 浅析设计与内容呈现的关系

    2009-08-06 18:12:00
  • SQL server不支持utf8 php却用utf8的矛盾问题解决方法

    2023-07-20 12:01:28
  • Python中文分词库jieba(结巴分词)详细使用介绍

    2023-03-17 10:31:35
  • django 邮件发送模块smtp使用详解

    2021-09-24 23:04:15
  • Python中的左斜杠、右斜杠(正斜杠和反斜杠)

    2022-02-08 07:00:34
  • 网页设计三剑客

    2010-08-31 17:05:00
  • asp之家 网络编程 m.aspxhome.com