ValueError:视图限制最小值-0.5小于1,并且在绘制到条形图时是无效的Matplotlib日期值

时间:2018-12-09 12:15:06

标签: python matplotlib

仅当我尝试使用数据框中的multinomialnb_label值的value_counts()绘制条形图时,才出现此错误。 有时候还可以。我不知道如何解决该错误。

它是flask应用程序的一部分,一切正常,直到上线为止:

my_plot = multinomialnb_count.plot(kind='bar', legend=None)

以下是多项式nb_count的详细信息:

[1395 rows x 15 columns]
bug report        721
question          492
praise            112
noise              49
other feedback     21
Name: multinomialnb_label, dtype: int64

有什么想法吗?

import matplotlib
matplotlib.use('Agg')
import pandas as pd
import numpy as np
import pymysql.cursors
import sqlalchemy
import datetime
import sys
import re
from wordcloud import WordCloud, STOPWORDS
import preprocessor as p
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
from nltk.collocations import *
from nltk.stem import WordNetLemmatizer
import matplotlib.pyplot as plt, mpld3
import matplotlib.dates as dates
import collections
import contractions
from matplotlib import rcParams

def make_charts(username):

    rcParams.update({'figure.autolayout': True})

    username = username
    new_db_name = username + "_predicted_tweets"

    # Get tweets from MYSQL database
    dbServerName = "localhost"
    dbUser = "root"
    dbPassword = "woodycool123"
    dbName = "azure_support_tweets"

    engine = sqlalchemy.create_engine('mysql+pymysql://root:woodycool123@localhost:3306/azure_support_tweets')
    df = pd.read_sql_table(new_db_name, engine)
    data = pd.DataFrame(df)

    multinomialnb_count = data['multinomialnb_label'].value_counts()
    my_plot = multinomialnb_count.plot(kind='bar', legend=None)

错误:

ValueError: view limit minimum -0.5 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

1 个答案:

答案 0 :(得分:1)

解决了!就像用plt.close().

关闭函数末尾的plt一样简单

这是因为为plt配置了更远的时间序列,然后在重新运行脚本时使用了它。我以为它将清除所有值,但是您必须关闭该plt才能使用新值!谢谢