How to make overlay plots of a variable, but every plot than i want to make has a different length of data

房东的猫 提交于 2020-01-25 07:25:45

问题


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

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')
`

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:

enter image description here

The graph that i want is this enter image description here

Mi data is represented in this form

enter image description here

and this are the formats

enter image description here


回答1:


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

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.

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



来源:https://stackoverflow.com/questions/58367119/how-to-make-overlay-plots-of-a-variable-but-every-plot-than-i-want-to-make-has

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!