如何制作变量的叠加图,但是每个比我想制作的图都有不同长度的数据 [英] How to make overlay plots of a variable, but every plot than i want to make has a different length of data

查看:87
本文介绍了如何制作变量的叠加图,但是每个比我想制作的图都有不同长度的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖30个图,每个图都是一天的温度,以便最后比较温度的变化以及一天之间的差异,问题是当我分开时数据(以30天为间隔)以熊猫为单位,每天的数据集具有不同的长度,例如第一天的温度数据为54977,第二天的数据为54988 ant,第三天的数据也不同,因此我在履历表中想要的是:覆盖30个图,并且在所得图形中,x轴使用第一天的时间刻度,其他29个图恰好与这些刻度匹配,并将数据减少到图中的极限,以使它们全部从终点开始其他如果有几个小时或数据丢失都没关系,我只想做这样的事情(见上图).

I want to overlay 30 plots, each of those is the Temperature of one day, to make at the end a comparison of the develop of the Temperature and how much differ from one day to another , the problem is that when i separate the data(separate the 30 days) in pandas, every day data set has different length,for example the first day has 54977 Temperature data , and the second day has 54988 ant the third also differ so the thing I want in resume is: overlay 30 plots and in the resultant graphic the x axis use the time ticks of the first day, and the other 29 plots just match to those ticks and reduce the data to a limit in the plot to make them all start from a point a finish in other it doesnt matter if some hours or data get lost, i just want to make something like this(see last image).

到目前为止的代码是这样,我在python中不是很好,所以不要判断我的长代码

The code so far is this, im not very good in python so dont judge my long code

`
import pandas as pd
from datetime import date
import datetime as dt
import calendar
import numpy as np
import pylab as plt 
import matplotlib.ticker as ticker
import seaborn as sns
>
datos = pd.read_csv("Jun2018T.txt", sep = ',', names=('Fecha', 'Hora', 'RADNETA', 'RADCORENT', 'RADCORSAL', 'RADINFENT', 'RADINFSAL', 'TEMP'))
>
datos['Hora'] = datos['Hora'].str[:9]
datos['Hora']
>
Dia01Jun2018 = datos[datos['Fecha'] == "2018-06-01"]
>
tiempo01=Dia01Jun2018['Hora']
temp01=Dia01Jun2018['TEMP']
>
imagen = plt.figure(figsize=(25,10))
plt.plot(tiempo01,temp01)
plt.xticks(np.arange(0, 54977, 7000)) #the number 54977 is the last data that the first day has, the second day has a different length an so on with the rest of the days
plt.xlabel("Tiempo (H:M:S)(Formato 24 Horas)")
plt.ylabel("Temperatura (K)")
plt.title("Día 01 Jun 2018")
plt.show()
imagen.savefig('D1JUN2018')
`

上面的代码每天重复一次,也许一个周期会更快,但是我对python的处理不是很好.

The code above repeats for every day, maybe with a cycle is more quickly but i don handle python very good.

此图的结果是下一张图:

And the result of this is this graph is the next one:

在此处输入图片描述

我想要的图形是这个 在此处输入图片描述

The graph that i want is this enter image description here

Mi数据以这种形式表示

Mi data is represented in this form

在此处输入图片描述

这是格式

在此处输入图片描述

推荐答案

如果我正确地理解了您的问题,即您想在一天内绘制所有图表,则必须在前几天生成一个数字plt.plot()您最终将图像plt.show()包括之前进行的所有绘图.尝试如下所示的内容:

if I understood your question right, that you want to plot all days in a single plot, you have togenerate one figure, plt.plot() all days before you finally plt.show() the image including all plots made before. Try something like shown below:

(因为我不知道您的数据,所以我不知道这段代码是否有效.至少应该清楚这个概念.)

(as I don't know your data, I don't know if this code would work. the concept should be clear at least.)

import pandas as pd
from datetime import date
import datetime as dt
import calendar
import numpy as np
import pylab as plt 
import matplotlib.ticker as ticker
import seaborn as sns
>
datos = pd.read_csv("Jun2018T.txt", sep = ',', names=('Fecha', 'Hora', 'RADNETA', 'RADCORENT', 'RADCORSAL', 'RADINFENT', 'RADINFSAL', 'TEMP'))
>
datos['Hora'] = datos['Hora'].str[:9]

>

imagen = plt.figure(figsize=(25,10))

for day in range(1,31):
    dia = datos[datos['Fecha'] == "2018-06-"+(f"{day:02d}")]
    tiempo= pd.to_datetime(dia['HORA'], format='%H:%M:%S').dt.time
    temp= dia['TEMP']
    plt.plot(tiempo, temp)

#plt.xticks(np.arange(0, 54977, 7000)) 
plt.xlabel("Tiempo (H:M:S)(Formato 24 Horas)")
plt.ylabel("Temperatura (K)")
plt.title("Jun 2018")
plt.show()
imagen.savefig('JUN2018')

对于问题的第二部分: 由于数据是带有时间戳存储的,因此可以将其转换为熊猫时间对象.使用它们进行绘图,x轴不应再有偏移.我已经修改了上面代码中的tiempo =...分配.

For the second part of your question: as your data is stored with an timestamp, you can transform it to pandas time objects. Using them for plots, the x-axis should not have an offset anymore. I've modified the tiempo =... assignment in the code above.

x-tics现在应该会自动进入时间模式.

The x-tics should automatically be in time mode now.

这篇关于如何制作变量的叠加图,但是每个比我想制作的图都有不同长度的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆