stage2——6

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

df = pd.read_csv("./911.csv")
#添加列,表示分类
tem_list = df["title"].str.split(": ").tolist()
cate_list = [i[0] for i in tem_list]
df["cate"] = pd.DataFrame(np.array(cate_list).reshape((df.shape[0],1)))
#把时间字符串设置为时间类型,并设置为索引
df["timeStamp"] = pd.to_datetime(df["timeStamp"])
df.set_index("timeStamp",inplace=True)

#print(type(df["cate"])) #结果为<class 'pandas.core.series.Series'>
plt.figure(figsize=(20,8),dpi=80)
#分组
for group_name,group_data in df.groupby(by="cate"):
    count_by_month = group_data.resample("M").count()["title"]
    #_x = count_by_month.index
    #_y = count_by_month.values
    #print(count_by_month)
    #print(group_name)
    #print(group_data)
    break
#    _x = [i.strftime("%Y-%M-%D") for i in _x]
#    plt.plot(range(len(_x)),_y,label=group_name)

#plt.xticks(range(len(_x)),_x,rotation = 45)
#plt.legend(loc="best")
#plt.show()

import pandas as pd
from matplotlib import pyplot as plt


file_path = "./BeijingPM20100101_20151231.csv"
df = pd.read_csv(file_path)
#使用PeriodIndex()把分开的时间数据转换为 pandas的时间类型
period = pd.PeriodIndex(year=df["year"],month=df["month"],day=df["day"],hour=df["hour"],freq="H")
df["datetime"] = period
print(df)
#把datatime设置为索引
df.set_index("datetime",inplace=True)
#进行降频重采样
df = df.resample("7D").mean()

#处理缺失数据,删除缺失值
#data = df["PM_US Post"].dropna()
#data_china = df["PM_Dongsi"].dropna()
data = df["PM_US Post"]         #进行降频重采样后,已经使用均值(mean)作为数据,故已经没有缺失值,所以不需要删除缺失值
data_china = df["PM_Dongsi"]    #进行降频重采样后,已经使用均值(mean)作为数据,故已经没有缺失值,所以不需要删除缺失值
#设置x轴y轴
_x = data.index     #x轴坐标为data的索引
_x = [i.strftime("%Y-%M-%d") for i in _x]       #将时间格式化,自定义时间格式
_x_china = data_china.index        #x轴坐标为data_china的索引
_x_china = [i.strftime("%Y-%M-%d") for i in _x_china]   #将时间格式化,自定义时间格式
_y = data.values      #y轴坐标为data的值
_y_china = data_china.values        #y轴坐标为data_china的值

#设置图片大小
plt.figure(figsize=(20,8),dpi=80)
#绘图
plt.plot(range(len(_x)),_y,label="US_POST",alpha=0.7)

plt.plot(range(len(_x_china)),_y_china,label="CN_POST",alpha=0.7)
#设置x轴信息
plt.xticks(range(0,len(_x),10),list(_x)[::10],rotation=45)
#添加图例
plt.legend(loc= "best")

plt.show()

mport numpy as np
from matplotlib import pyplot as plt

runtime_data= np.array([8.1,7.0,7.3,7.2,6.2,6.1,8.3,6.4,7.1,7.0,7.5,7.9,7.7,9.0,8.5,8.7,8.1,8.9,8.2,8.4,8.7,1.9,3.5,4.7,5.6])
max_runtime = runtime_data.max()
min_runtime = runtime_data.min()

#print(min_runtime,max_runtime)
#设置不等宽组距,hist方法中取到的会是一个[1.9,3.5]左开右闭的半开区间
num_bin_list = [1.9,3.5]
i = 3.5
while i <max_runtime:
    i +=0.5
    num_bin_list.append(i)
print(num_bin_list)
print(runtime_data)

plt.figure(figsize=(20,8),dpi=80)

plt.hist(runtime_data,num_bin_list)

plt.xticks(num_bin_list)

#plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值