本文介绍了ValueError:视图限制最小值-35738.3640567小于1,并且是无效的Matplotlib日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是matplotlib的初学者.我正在尝试使用matplotlib.pyplot绘制数据框.问题是,每次我尝试绘制它时,都会出现以下错误:

im a beginner in matplotlib. Im trying to plot a dataframe using matplotlib.pyplot. The problem is that everytime I try to plot it i get the following error:

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.

根据该错误,看来好像datetime列中有一个非datetime值,但没有.

According to the error, it seems to be like theres a non-datetime value in the datetime column, but there isnt.

我尝试使用pd.to_datetime()并尝试将时间戳的格式更改为pd.to_datetime(df_google['datetime'], format = '%d/%m/%Y'),但没有任何改变.

Ive tried using pd.to_datetime() and try to change the format of the timestamp to pd.to_datetime(df_google['datetime'], format = '%d/%m/%Y') but nothing changes.

这是我要使用的代码:

import matplotlib.pyplot as plt

df_google.plot()
plt.show()

df_google是具有列['datetime','price']的数据框,其中一些值如下:

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 says theres a non-datetime value when every value is a datetime type value? How can I plot this dataframe?

推荐答案

给出以下数据:

   datetime        price
 2018-05-15  1079.229980
 2018-05-16  1081.770020
 2018-05-17  1078.589966
 2018-05-18  1066.359985
 2018-05-21  1079.579956
 2018-05-22  1069.729980
 2018-05-23  1079.689941
 2018-05-24  1079.239990
 2018-05-25  1075.660034
 2018-05-29  1060.319946

datetime列设置为Datetime格式并设置为索引:

    如果列标题中没有空格,则可以使用.访问
  • 列名称
    • df_google.datetime而不是df_google['datetime']
    • Set the datetime column to a Datetime format and set as index:

      • Column names can be accessed with a ., if there are no spaces in the column header
        • df_google.datetime instead of df_google['datetime']
        • import pandas as pd
          import matplotlib.pyplot as plt
          
          df_google.datetime = pd.to_datetime(df.datetime)
          df_google.set_index('datetime', inplace=True)
          
          print(df_google.info())
          
          <class 'pandas.core.frame.DataFrame'>
          DatetimeIndex: 10 entries, 2018-05-15 to 2018-05-29
          Data columns (total 1 columns):
          price    10 non-null float64
          dtypes: float64(1)
          memory usage: 160.0 bytes
          

          情节:

          df_google.plot()
          plt.show()
          

          • 有大量替代绘图工具的生态系统,但是df.plot()适合查看数据.
            • PyViz
              • There's a substantial ecosystem of alternative plotting tools, but df.plot() is fine for getting a look at the data.
                • PyViz
                • 这篇关于ValueError:视图限制最小值-35738.3640567小于1,并且是无效的Matplotlib日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 19:57