我正在使用seaborn模块生成类似于以下示例的绘图。
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"]])
我的df看起来像:
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
我想创建一个具有不同天气类别的seaborn stripplot,类似于下面的情节。
也称为词汇弥散图。
任何帮助都会很棒!
我可以在此处找到csv格式的示例数据集 https://www.dropbox.com/s/ulzz5x3rsl2yjd5/sample_data.csv?dl=0
答案 0 :(得分:3)
您必须使用stripplot
。首先,您必须正确读取数据的datetime列,然后绘制它:
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()