DateFormatter returning wrong dates - Matplotlib/Pandas [closed]

2024/7/7 8:05:42

I am trying to plot some data using matplotlib and pandas. However when using the DateFormatter, dates are being rendered incorrectly depending on what I filter out of the DataFrame:

The dates in the two examples below render with matplotlib as 'August 20 00 2013', as expected:

df['metric2'].plot()
ax = gca()
ax.xaxis.set_major_formatter(DateFormatter('%B %d %H %Y'))
draw()df[df['metric1']>1000]['metric2'].plot()
ax = gca()
ax.xaxis.set_major_formatter(DateFormatter('%B %d %H %Y'))
draw()

However using the code below, the dates are being rendered as 'February 01 00 1048':

df[df['browser']=='Chrome/29']['metric2'].plot()
ax = gca()
ax.xaxis.set_major_formatter(DateFormatter('%B %d %H %Y'))
draw()
Answer

We need to have a concrete set of data and a program to refer to. No problems here:

data.txt:

2013-08-18 00   IE  1000    500 3000
2013-08-19 00   FF  2000    250 6000
2013-08-20 00   Opera   3000    450 9000
2001-03-21 00   Chrome/29   3000    450 9000
2013-08-21 00   Chrome/29   3000    450 9000
2014-01-22 00   Chrome/29   3000    750 9000

.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as md
import datetime as dtdf = pd.read_table('data.txt', index_col=0, parse_dates=True,date_parser=lambda s: dt.datetime.strptime(s, '%Y-%m-%d %H'),header=None,names=['browser', 'metric1', 'metric2', 'metric3']
)print dfdf[df['browser']=='Chrome/29']['metric2'].plot()
ax = plt.gca()
ax.xaxis.set_major_formatter(md.DateFormatter('%B %d %H %Y'))
plt.draw()
plt.show()--output:--browser  metric1  metric2  metric3
2013-08-18         IE     1000      500     3000
2013-08-19         FF     2000      250     6000
2013-08-20      Opera     3000      450     9000
2001-03-21  Chrome/29     3000      450     9000
2013-08-21  Chrome/29     3000      450     9000
2014-01-22  Chrome/29     3000      750     9000

enter image description here

And with the axes adjusted so you can see the points better(setting date range of x axis, setting range of y axis):

...
df[df['browser']=='Chrome/29']['metric2'].plot(style='r--')
ax = plt.gca()
ax.xaxis.set_major_formatter(md.DateFormatter('%B %d %H %Y'))ax.set_xlim(dt.datetime(2000, 1, 1,), dt.datetime(2017, 1, 1))
ax.set_ylim(400, 1000)
...
...

enter image description here

As long as you refuse to post a minimal example along with the data that produces the output you don't want...

https://en.xdnf.cn/q/120663.html

Related Q&A

What does the error IndentationError: expected an indented block in python mean? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

How to insert character in csv cell in python?

Im new with python. Here is my csv file :data;data;name surname; data; data data;data;name surname; data; data data;data;name surname; data; data data;data;name surname; data; dataThe thing that I want…

How can you initialise an instance in a Kivy screen widget

I am trying to access an instance variable named self.localId in my kivy screen and it keeps saying the saying the instance doesnt exist after i have initialised it. I know I have an error In my code b…

is a mathematical operator classed as an interger in python

in python is a mathematical operator classed as an interger. for example why isnt this code workingimport randomscore = 0 randomnumberforq = (random.randint(1,10)) randomoperator = (random.randint(0,2)…

Fix a function returning duplicates over time?

I have a function here that returns a 4 digit string. The problem is that when I run the function like 500 times or more, it starts to return duplicates. How to avoid that?My Function:import random de…

Pandas method corr() use not all features

I have dataframe with shape (335539, 26). So I have 26 features. But when i use data.corr() I get a 12 x 12 matrix.What can be wrong? `

int to datetime in Python [duplicate]

This question already has answers here:Convert string "Jun 1 2005 1:33PM" into datetime(26 answers)Parsing datetime in Python..?(2 answers)Closed 5 years ago.Im receiving data from the port.…

How to extract the historical tweets from twitter API? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

ValueError: invalid literal for int() with base 16: [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Modify list in Python [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…