Python Forum
Help: how to deal with a invalid Matplotlib date value when use {gca()}
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help: how to deal with a invalid Matplotlib date value when use {gca()}
#1
Hi,

I am using Python 3.6.5.

When I use the following code to create a Line plot with 2 columns for a data-frame:
import pandas as pd
import xlrd

xlsx_source = 'https://community.tableau.com/servlet/JiveServlet/downloadBody/1236-102-2-15278/Sample%20-%20Superstore.xls'

df = pd.read_excel(xlsx_source, sheet_name='Orders')

df.head()

import matplotlib.pyplot as plt

ax = plt.gca()

df.plot(kind='line', x='Ship Date', y='Profit', color='red', ax=ax)
df.plot(kind='line', x='Ship Date', y='Sales', color='blue', ax=ax)
plt.show()
However, get an error:
...ValueError: view limit minimum -36835.2125 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...


Here, the type of [Ship Date] is datetime64[ns].

It looks like this error is caused by ax = plt.gca() because after I remove it (and related ax=ax), the code can run smoothly but output 2 line plots.

How to solve this problem if I want to make a Line plot with multiple columns?
Reply
#2
Hi,

Your code seems to work on my computer. I just have change the xls typ eto csv. You might want to try that line to import csv:
df = pd.read_csv(csv_source)

What version of matplotlib do you use?

Otherwise I would suggest you to build your figure like this:

f, ax = plt.subplots(1)

So you don't have to use ax = plt.gca(). Then continue:

df.plot(kind='line', x='Ship Date', y='Profit', color='red', ax=ax)
df.plot(kind='line', x='Ship Date', y='Sales', color='blue', ax=ax)
plt.show()

But I doubt that is the root cause of the error message you got, it's more like a workaround...
Reply
#3
Great! Thank you!

I am using matplotlib 2.2.2.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  replace nan values by mean group by date.year, date.month wissam1974 5 8,497 Feb-19-2020, 06:25 PM
Last Post: AnkitGupta
  %matplotlib inline , invalid syntax yamoon 1 13,083 Jul-12-2018, 07:22 AM
Last Post: volcano63
  Finding date count from a list of date range in pandas trillerducas72 0 2,757 May-24-2018, 02:30 AM
Last Post: trillerducas72
  how to deal with varying value-counts().keys() in pandas dataframe dilmailid 4 4,487 May-16-2018, 09:28 AM
Last Post: dilmailid

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020