Help: how to deal with a invalid Matplotlib date value when use {gca()} - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Help: how to deal with a invalid Matplotlib date value when use {gca()} (/thread-13470.html) |
Help: how to deal with a invalid Matplotlib date value when use {gca()} - pythopen - Oct-16-2018 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? RE: Help: how to deal with a invalid Matplotlib date value when use {gca()} - Giska - Oct-17-2018 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... RE: Help: how to deal with a invalid Matplotlib date value when use {gca()} - pythopen - Oct-18-2018 Great! Thank you! I am using matplotlib 2.2.2. |