Lexical dispersion plot is seaborn

2019-05-15 05:41发布

问题:

I am using the seaborn module to produce a plot similar to the example below.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
location = "/global/scratch/umalmonj/WRF/juris/golden_hourly_manual_obs.csv"

df = pd.read_csv(location,usecols= ["Year","Month","Day","Time","Weather"],parse_dates=[["Year","Month","Day","Time"]])

I have a df that looks like:

     Year_Month_Day_Time        Weather
0    2010-01-01 00:00:00            NaN
1    2010-01-01 01:00:00            NaN
2    2010-01-01 02:00:00            NaN
..    
7    2010-01-01 07:00:00           Snow
8    2010-01-01 08:00:00           Snow
9    2010-01-01 09:00:00   Snow Showers
..
18   2010-01-01 18:00:00            NaN
19   2010-01-01 19:00:00            NaN
20   2010-01-01 20:00:00            NaN
...                  ...            ...
2861 2010-04-30 05:00:00   Mainly Clear
2862 2010-04-30 06:00:00   Mainly Clear
2863 2010-04-30 07:00:00  Mostly Cloudy

I want to create a seaborn stripplot with the different weather categories something similar to the following plot.

Also known as a lexical dispersion plot.

Any help would be great!

My sample dataset in a csv format can be found here https://www.dropbox.com/s/ulzz5x3rsl2yjd5/sample_data.csv?dl=0

回答1:

You have to use stripplot. Firstly, you have to properly read datetime column of your data and then plot it:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# datetime parser
dateparse = lambda x: pd.datetime.strptime(x, '%y/%m/%d %H:%M')
df = pd.read_csv('./sample_data.csv',parse_dates=['DateTime'], date_parser=dateparse)

# set size of figure
plt.figure(figsize=(22,6))
# use horizontal stripplot with x marker size of 5
sns.stripplot(y='Weather',x='DateTime', data=df,
 orient='h', marker='X', color='navy', size=5)
# rotate x tick labels
plt.xticks(rotation=15)
# remover borders of plot
plt.tight_layout()
plt.show()

Figure is clickable