3

I am getting this error only sometimes when I try to plot a bar chart with the value_counts() of the multinomialnb_label values in the dataframe. Other times it is fine. I can't figure out how to fix the error.

It is part of a flask application and everything works fine until it hits the line:

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

Here is the multinomialnb_count details:

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

Any ideas?

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)

Error:

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
beth
  • 425
  • 1
  • 8
  • 16

1 Answers1

7

Solved it! It was as simple as just closing the plt at the end of the function with plt.close().

It was because the plt was configured for a time series further down and then when the script was rerun it was using that. I assumed it would clear all the values but you have to close that plt to then use a new one! Thanks

beth
  • 425
  • 1
  • 8
  • 16