小编Jua*_*eto的帖子

如何为 Seaborn displot 指定日期 bin 范围

问题陈述

我正在创建从 1870 年开始的每N年洪水事件的分布图。我使用的是 Pandas 和 Seaborn。我需要帮助...

  1. 使用 时指定每个 bin 的日期范围sns.displot,以及
  2. 沿x轴清楚地表示我的 bin 尺寸规格。

为了澄清这个问题,这里是我正在使用的数据、我尝试过的数据以及所需输出的描述。

数据

我使用的数据可从美国天气服务获得。

import pandas as pd
import bs4
import urllib.request
link = "https://water.weather.gov/ahps2/crests.php?wfo=jan&gage=jacm6&crest_type=historic"

webpage=str(urllib.request.urlopen(link).read())
soup = bs4.BeautifulSoup(webpage)

tbl = soup.find('div', class_='water_information')
vals = tbl.get_text().split(r'\n')

tcdf = pd.Series(vals).str.extractall(r'\((?P<Rank>\d+)\)\s(?P<Stage>\d+.\d+)\sft\son\s(?P<Date>\d{2}\/\d{2}\/\d{4})')\
    .reset_index(drop=True)

tcdf['Stage'] = tcdf.Stage.astype(float)
total_crests_events = len(tcdf)
tcdf['Rank'] = tcdf.Rank.astype(int)
tcdf['Date'] = pd.to_datetime(tcdf.Date)
Run Code Online (Sandbox Code Playgroud)

什么有效

我可以使用 Seaborn 绘制数据displot,并且可以使用命令操纵垃圾箱的数量bins

第二张图片更接近我想要的输出。然而,我认为垃圾箱的起点和终点并不清楚。例如,前两个 bin(从左到右阅读)清楚地开始于 1880 年之前并结束于 1880 年之后,但确切的年份不清楚。

import seaborn as sns
# …
Run Code Online (Sandbox Code Playgroud)

python datetime pandas seaborn

4
推荐指数
1
解决办法
1343
查看次数

标签 统计

datetime ×1

pandas ×1

python ×1

seaborn ×1