简体   繁体   中英

matplotlib/seaborn scatter plot with datetime object on x-axis and days with multiple entries

I have a data frame and would like to make a scatter plot of how long it took for a request to be completed days on the y-axis and the day the request was filed ( Received , which is a datetime object) on the x-axis.

Someone values of 'Received' have two entries because sometimes two requests were filed on the same day.

Here are some of my data and the code I have tried:

Received          Days
2012-08-01        41.0 
2014-12-31       692.0
2015-02-25       621.0
2015-10-15       111.0

sns.regplot(x=simple_denied["Received"], y=simple_denied["days"], marker="+", fit_reg=False)


plt.plot('Received','days', simple_denied, color='black')

Let's start by setting up your data. I actually added another date '2014-12-31' to your example dataset, so that we can verify that our plotting routine works when we have multiple requests received on the same day:

import matplotlib.pyplot as plt
plt.style.use('seaborn')
import pandas as pd
import numpy as np

dates = np.array(['2012-08-01', '2014-12-31',
                  '2014-12-31', '2015-02-25',
                  '2015-10-15'], dtype='datetime64')

days = np.array([41, 692, 50, 621, 111])

df = pd.DataFrame({'Received' : dates, 'Days' : days})

The dataframe created should hopefully approximate what you have. Producing the scatter plot you desire is now straight forward:

fig, ax = plt.subplots(1, 1)

ax.scatter(df['Received'], df['Days'], marker='+')
ax.set_xlabel("Receieved")
ax.set_ylabel("Days")

This gave me the following plot:

在此处输入图片说明 As noted by @ImportanceOfBeingErnest in the comments below, you need a recent version of pandas for this routine to work.

You hit two cases which don't work. sns.regplot would not work with dates. And plt.plot will need to have the data specified (it cannot know which data to use just by the name of the columns).

So any of the following would provide you a scatter plot of the data

  • sns.scatterplot(x="Received", y="days", data=simple_denied, marker="+")
  • sns.scatterplot(x=simple_denied["Received"], y=simple_denied["days"], marker="+")

  • plt.scatter(simple_denied["Received"].values, simple_denied["days"].values, marker="+")

  • plt.plot(simple_denied["Received"].values, simple_denied["days"].values, marker="+", ls="")

  • plt.plot("Received", "days", data=simple_denied, marker="+", ls="")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM