Reputation: 31
I got data from a meteorological station to plot including temperature-, pressure- and radiation- measurements. When trying to plot the different datapoints I always get this error...even though the code works for another station with the exact same parameters but (obviously) different measurements. I have tryed everything but cant find the source of the problem. I hope someone can help me!
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt
datenmodelo = pd.read_csv('1163655_010319_OZ_ALL_2.csv', sep=';', usecols=['Monat','Datum', 'Zeit', 'T_oM', 'RH_oM', 'T_uM', 'RH_uM', 'p_M', 'G_M', 'PAR_M'])
daten_si_M = datenmodelo.set_index(['Datum'])
dfmt1 = mdates.DateFormatter('%H:%M')
x_date_M = [dt.datetime.strptime(d,'%d.%m.%Y').date() for d in dates_M]
a = 0
start = 0
end = 1
for daysM in dates_M[start:end]:
# Definition aller Funktionswerte/Größen (eingeschlossen NaN)
T_uMd= pd.to_numeric(daten_si_M.loc[daysM].T_uM, errors='coerce')
T_oMd = pd.to_numeric(daten_si_M.loc[daysM].T_oM, errors='coerce')
RH_uMd = pd.to_numeric(daten_si_M.loc[daysM].RH_uM, errors='coerce')
RH_oMd = pd.to_numeric(daten_si_M.loc[daysM].RH_oM, errors='coerce')
G_Md = pd.to_numeric(daten_si_M.loc[daysM].G_M, errors='coerce')
p_Md = pd.to_numeric(p_M_korr.loc[daysM], errors='coerce')
PAR_Md= pd.to_numeric(daten_si_M.loc[daysM].PAR_M, errors='coerce')
x_time_M = pd.to_datetime(daten_si_M.loc[daysM].Zeit)
f, axarr = plt.subplots(4,1)
f.set_size_inches(15, 20)
# Titel der Graphen und Achsen:
axarr[0].set_title('Modelo - %s'%daysM, fontsize=14, fontweight='bold')
axarr[3].set_xlabel('Uhrzeit', fontweight='bold')
axarr[1].set_ylabel('T [°C]', fontweight='bold')
axarr[2].set_ylabel('RH [%]', fontweight='bold')
axarr[0].set_ylabel('G [W/m^2]', fontweight='bold')
axarr[3].set_ylabel('p [hPa]', fontweight='bold')
# Formatierung der Achsen:
axarr[0].xaxis.set_major_formatter(dfmt1)
axarr[1].xaxis.set_major_formatter(dfmt1)
axarr[2].xaxis.set_major_formatter(dfmt1)
axarr[3].xaxis.set_major_formatter(dfmt1)
# Plot der Variablen und Label der Kurve:
axarr[1].plot(x_time_M, T_uMd,'r', label='Temperatur (unten)')
axarr[1].plot(x_time_M, T_oMd, color='indigo', label='Temperatur (oben)')
axarr[2].plot(x_time_M, RH_uMd,'r', label='Relative Feuchte (unten)')
axarr[2].plot(x_time_M, RH_oMd,color='indigo', label='Relative Feuchte (oben)')
axarr[0].plot(x_time_M, G_Md,'b', label='Globalstrahlung')
axarr[0].plot(x_time_M, PAR_Md,'c', label='Photosynthetische Strahlung')
axarr[3].plot(x_time_M, p_Md,'g', label='Druck')
# Positionierung der Labels:
axarr[0].legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
axarr[1].legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
axarr[2].legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
axarr[3].legend(loc='center left', bbox_to_anchor=(1.05, 0.5))
# Gitter:
plt.rc('grid', linestyle="dashed", color='b', alpha=0.5, linewidth=0.5)
plt.rcParams['axes.grid'] = True
# Plot:
plt.savefig('Modelo (GTRHp) - %s.png' %(str(daysM)), bbox_inches='tight', dpi=300)
plt.close()
a += 1
plt.show()
Upvotes: 3
Views: 1975
Reputation: 574
Your symptom looks similar to this known bug. I can't confirm because your code looks incomplete. You don't show what dates_M
is, for example.
Ignoring that, I'd look at the values in x_time_M
. I expect they're all something like numpy.nan
. Then look at daten_si_M
to find out why.
Regardless, here's a much simpler case of the bug I think you (and I) are seeing.
import matplotlib.pyplot as plt
import numpy as np
fig,ax = plt.subplots()
ax.xaxis_date()
ax.plot([np.nan])
plt.show()
That triggers the same exception you saw.
ValueError: view limit minimum -0.001 is less than 1 and is an invalid
Matplotlib date value. This often happens if you pass a non-datetime to
an axis that has datetime units
Changing the plot call to this works fine.
ax.plot([])
If I understand, matplotlib
date values cannot be less than 1. In this example, all the coordinates (only one here) are numpy.nan
, which causes the view limits to be set to (-0.001, 0.001), neither of which can be used as a date, triggering the exception.
I'm having the same problem in my own code, which uses matplotlib
2.2.2, and don't have the option of upgrading. The only workaround I've come up with is to set the limits on the x-axis when I detect this case.
...
ax.set_xlim(now,now+oneDay)
...
The range makes no difference in my case, because there's nothing better, given that all the x-coordinates are numpy.nan
.
Good luck.
Upvotes: 1