假设条件
我想您是从一个类似于保存在
Vanuatu Earthquakes 2018-2019.csv文件中的数据帧开始的:
import pandas as pdimport numpy as nptime = pd.date_range(start = '01-01-2020', end = '31-03-2020', freq = 'D')df = pd.Dataframe({'date': list(map(lambda x: str(x), time)), 'mag': np.random.random(len(time))})输出:
date mag0 2020-01-01 00:00:00 0.9400401 2020-01-02 00:00:00 0.7655702 2020-01-03 00:00:00 0.9518393 2020-01-04 00:00:00 0.7081724 2020-01-05 00:00:00 0.7050325 2020-01-06 00:00:00 0.8575006 2020-01-07 00:00:00 0.8664187 2020-01-08 00:00:00 0.3632878 2020-01-09 00:00:00 0.2896159 2020-01-10 00:00:00 0.741499
绘图:
import seaborn as snsimport matplotlib.pyplot as pltfig, ax = plt.subplots(figsize = (15, 7))sns.lineplot(ax = ax, x='date', y='mag', data=df).set_title('Earthquake magnitude May 2018-2019')plt.xlabel('Date')plt.ylabel('Magnitude (Mw)')plt.show()回答
您应该做一系列的事情:
- 首先,你得到的标签是密度,因为你的
'date'
值str
类型,你需要将它们转换为datetime
通过df['date'] = pd.to_datetime(df['date'], format = '%Y-%m-%d')
这样,您的x轴就是一个
datetime类型,上面的图将变成这样:
然后,您必须调整刻度线;对于主要刻度,您应该设置:
import matplotlib.dates as md
specify the position of the major ticks at the beginning of the week
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday = 1))
specify the format of the labels as ‘year-month-day’
ax.xaxis.set_major_formatter(md.DateFormatter(‘%Y-%m-%d’))
(optional) rotate by 90° the labels in order to improve their spacing
plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90)
对于较小的滴答声:
# specify the position of the minor ticks at each dayax.xaxis.set_minor_locator(md.DayLocator(interval = 1))
您可以选择使用以下方法编辑刻度线的长度:
ax.tick_params(axis = 'x', which = 'major', length = 10)ax.tick_params(axis = 'x', which = 'minor', length = 5)
因此最终的情节将变为:
整个代码
# import required packagesimport pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltimport matplotlib.dates as md# read the dataframedf = pd.read_csv('Vanuatu Earthquakes 2018-2019.csv')# convert 'date' column type from str to datetimedf['date'] = pd.to_datetime(df['date'], format = '%Y-%m-%d')# prepare the figurefig, ax = plt.subplots(figsize = (15, 7))# set up the plotsns.lineplot(ax = ax, x='date', y='mag', data=df).set_title('Earthquake magnitude May 2018-2019')# specify the position of the major ticks at the beginning of the weekax.xaxis.set_major_locator(md.WeekdayLocator(byweekday = 1))# specify the format of the labels as 'year-month-day'ax.xaxis.set_major_formatter(md.DateFormatter('%Y-%m-%d'))# (optional) rotate by 90° the labels in order to improve their spacingplt.setp(ax.xaxis.get_majorticklabels(), rotation = 90)# specify the position of the minor ticks at each dayax.xaxis.set_minor_locator(md.DayLocator(interval = 1))# set ticks lengthax.tick_params(axis = 'x', which = 'major', length = 10)ax.tick_params(axis = 'x', which = 'minor', length = 5)# set axes labelsplt.xlabel('Date')plt.ylabel('Magnitude (Mw)')# show the plotplt.show()笔记
如果您注意图中的y轴,则会看到
'mag'值落在范围内
(0-1)。这是由于我使用生成了这些 伪造的 数据
'mag':np.random.random(len(time))。如果你读 你 从文件中的数据
Vanuatu Earthquakes2018-2019.csv,你会得到y轴上的正确值。尝试简单地复制 整个代码 部分中的 代码 。


![降低Seaborn线图中日期的x轴值密度?[更新] 降低Seaborn线图中日期的x轴值密度?[更新]](http://www.mshxw.com/aiimages/31/400144.png)
