简体   繁体   中英

Seaborn point plot using dates as x-axis

I am trying to produce a point plot of dataset grouped based on the date of data collection using Seaborn.

After playing with Seaborn point plot a little while, I realized an issue: these data were collected on different dates but the dates are not evenly spaced. It seems to me that during plotting, Seaborn was considering the dates as categorical variables.

产生的图像

I am wondering if there is a way for me to locate the data with spacing corresponding to the dates interval?

Try the following to convert your string dates into datetime objects

import time
import datetime as dt
from datetime import datetime

def dateconvert(Start_date):
    try:
        Start_date = int(Start_date)  
        Date_String = str(Start_date)
        dateconv = dt.datetime.strptime(Date_String,'%YYYY-%mm-%dd').strftime('%d/%m/%Y')
        return  dateconv
    except ValueError:
        return Start_date #or '' or whatever you want to return for NULL

It seems to me that Seaborn doesn't support datetime as the x variables, and all the dates are considered as categorical variables in this case. It might not be a big issue here but it prevents me from fitting a regression line to the dots.

I ended up using the matplotlib.errorbar function, with the median value and std calculated using numpy . It doesn't seem to be an ideal solution but it worked. (Still working on the regression trendline but I'm fine now.)

updated plot

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