ValueError: view limit minimum -35738.3640567 is less than 1 and is an invalid Matplotlib date value
Question:
I’m trying to plot a dataframe using matplotlib.pyplot
, but I get the following error when plotting:
ValueError: view limit minimum -35738.3640567 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units.
According to the error, it seems like there’s a non-datetime value in the 'datetime'
column, but there isn’t.
I’ve tried using pd.to_datetime()
to change the format of the timestamp with pd.to_datetime(df_google['datetime'], format = '%d/%m/%Y')
, but nothing changes.
This is the code:
import matplotlib.pyplot as plt
df_google.plot()
plt.show()
df_google
is a dataframe with columns ['datetime', 'price']
and some of the values are the following:
datetime price
0 2018-05-15 1079.229980
1 2018-05-16 1081.770020
2 2018-05-17 1078.589966
3 2018-05-18 1066.359985
4 2018-05-21 1079.579956
5 2018-05-22 1069.729980
6 2018-05-23 1079.689941
7 2018-05-24 1079.239990
8 2018-05-25 1075.660034
9 2018-05-29 1060.319946
Can someone try to help me understand this type of error? Why does it say there’s a non-datetime value when every value is a datetime Dtype
value? How can I plot this dataframe?
Answers:
- Set the
'datetime'
column to a datetime64[ns] Dtype
to resolve the error:
- Use
pandas.to_datetime
to convert the 'datetime'
column, and remember to assign the column back to itself, because this is not an inplace update.
pandas
is good at figuring out the format of datetimes, but it may be necessary to use the format=
option to specify the current format of the 'datetime'
column. See Convert Pandas Column to DateTime.
- Column names can be accessed with a
.
, if they do not contain special characters, and do not clash with built-in attributes/methods (e.g., index
, count
).
df_google.datetime
instead of df_google['datetime']
import pandas as pd
import matplotlib.pyplot as plt
# given the following data
data = {'datetime': ['2018-05-15', '2018-05-16', '2018-05-17', '2018-05-18', '2018-05-21', '2018-05-22', '2018-05-23', '2018-05-24', '2018-05-25', '2018-05-29'],
'price': [1079.22998, 1081.77002, 1078.589966, 1066.359985, 1079.579956, 1069.72998, 1079.689941, 1079.23999, 1075.660034, 1060.319946]}
df_google = pd.DataFrame(data)
# convert the datetime column to a datetime type and assign it back to the column
df_google.datetime = pd.to_datetime(df_google.datetime)
# display(df_google.head())
datetime price
0 2018-05-15 1079.229980
1 2018-05-16 1081.770020
2 2018-05-17 1078.589966
3 2018-05-18 1066.359985
4 2018-05-21 1079.579956
5 2018-05-22 1069.729980
6 2018-05-23 1079.689941
7 2018-05-24 1079.239990
8 2018-05-25 1075.660034
9 2018-05-29 1060.319946
Verify the 'datetime'
column is a datetime64[ns]
Dtype:
print(df_google.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 datetime 10 non-null datetime64[ns]
1 price 10 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage: 288.0 bytes
Plot:
- Specify the column to be the axis with
x=...
to plot.
df_google.plot(x='datetime')
plt.show()
- The column to be the x-axis can also be set as the index with
df.set_index('datetime', inplace=True)
, and then df.plot()
to plot, but setting the index is not necessary, and is irrelevant to the error.
- There’s a substantial ecosystem of alternative plotting tools, but
df.plot()
is fine for getting a look at the data.
Note:
- This solution was for this OP, however, there is a similar question, ValueError: view limit minimum -0.5 is less than 1 and is an invalid Matplotlib date value when plotting to a bar chart, with a different solution.
- Make sure
pandas
and matplotlib
are update.
- If you are using Anaconda, update with
conda upate --all
. Do not use pip
to update packages installed with Anaconda.
- If you are using a notebook, try restarting the kernel and clearing outputs.
I had the same problem just now. The output was
view limit minimum 0.0 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units
I set int
limit for my x-values, although I had pandas._libs.tslibs.timestamps.Timestamp
values in my ‘Release_Date’ column
with sns.axes_style('darkgrid'):
ax = sns.scatterplot(data=data_clean,
# Release_Date column has pandas._libs.tslibs.timestamps.Timestamp in it
x='Release_Date',
y='USD_Worldwide_Gross',
)
ax.set(ylim=(0, 3000000000),
# xlim caused problem, because of int values
xlim=(0, 450000000))
plt.show()
I’m trying to plot a dataframe using matplotlib.pyplot
, but I get the following error when plotting:
ValueError: view limit minimum -35738.3640567 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units.
According to the error, it seems like there’s a non-datetime value in the 'datetime'
column, but there isn’t.
I’ve tried using pd.to_datetime()
to change the format of the timestamp with pd.to_datetime(df_google['datetime'], format = '%d/%m/%Y')
, but nothing changes.
This is the code:
import matplotlib.pyplot as plt
df_google.plot()
plt.show()
df_google
is a dataframe with columns ['datetime', 'price']
and some of the values are the following:
datetime price
0 2018-05-15 1079.229980
1 2018-05-16 1081.770020
2 2018-05-17 1078.589966
3 2018-05-18 1066.359985
4 2018-05-21 1079.579956
5 2018-05-22 1069.729980
6 2018-05-23 1079.689941
7 2018-05-24 1079.239990
8 2018-05-25 1075.660034
9 2018-05-29 1060.319946
Can someone try to help me understand this type of error? Why does it say there’s a non-datetime value when every value is a datetime Dtype
value? How can I plot this dataframe?
- Set the
'datetime'
column to adatetime64[ns] Dtype
to resolve the error:- Use
pandas.to_datetime
to convert the'datetime'
column, and remember to assign the column back to itself, because this is not an inplace update. pandas
is good at figuring out the format of datetimes, but it may be necessary to use theformat=
option to specify the current format of the'datetime'
column. See Convert Pandas Column to DateTime.
- Use
- Column names can be accessed with a
.
, if they do not contain special characters, and do not clash with built-in attributes/methods (e.g.,index
,count
).df_google.datetime
instead ofdf_google['datetime']
import pandas as pd
import matplotlib.pyplot as plt
# given the following data
data = {'datetime': ['2018-05-15', '2018-05-16', '2018-05-17', '2018-05-18', '2018-05-21', '2018-05-22', '2018-05-23', '2018-05-24', '2018-05-25', '2018-05-29'],
'price': [1079.22998, 1081.77002, 1078.589966, 1066.359985, 1079.579956, 1069.72998, 1079.689941, 1079.23999, 1075.660034, 1060.319946]}
df_google = pd.DataFrame(data)
# convert the datetime column to a datetime type and assign it back to the column
df_google.datetime = pd.to_datetime(df_google.datetime)
# display(df_google.head())
datetime price
0 2018-05-15 1079.229980
1 2018-05-16 1081.770020
2 2018-05-17 1078.589966
3 2018-05-18 1066.359985
4 2018-05-21 1079.579956
5 2018-05-22 1069.729980
6 2018-05-23 1079.689941
7 2018-05-24 1079.239990
8 2018-05-25 1075.660034
9 2018-05-29 1060.319946
Verify the 'datetime'
column is a datetime64[ns]
Dtype:
print(df_google.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 datetime 10 non-null datetime64[ns]
1 price 10 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage: 288.0 bytes
Plot:
- Specify the column to be the axis with
x=...
to plot.
df_google.plot(x='datetime')
plt.show()
- The column to be the x-axis can also be set as the index with
df.set_index('datetime', inplace=True)
, and thendf.plot()
to plot, but setting the index is not necessary, and is irrelevant to the error. - There’s a substantial ecosystem of alternative plotting tools, but
df.plot()
is fine for getting a look at the data.
Note:
- This solution was for this OP, however, there is a similar question, ValueError: view limit minimum -0.5 is less than 1 and is an invalid Matplotlib date value when plotting to a bar chart, with a different solution.
- Make sure
pandas
andmatplotlib
are update.- If you are using Anaconda, update with
conda upate --all
. Do not usepip
to update packages installed with Anaconda.
- If you are using Anaconda, update with
- If you are using a notebook, try restarting the kernel and clearing outputs.
I had the same problem just now. The output was
view limit minimum 0.0 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units
I set int
limit for my x-values, although I had pandas._libs.tslibs.timestamps.Timestamp
values in my ‘Release_Date’ column
with sns.axes_style('darkgrid'):
ax = sns.scatterplot(data=data_clean,
# Release_Date column has pandas._libs.tslibs.timestamps.Timestamp in it
x='Release_Date',
y='USD_Worldwide_Gross',
)
ax.set(ylim=(0, 3000000000),
# xlim caused problem, because of int values
xlim=(0, 450000000))
plt.show()