降低seaborn测线图上日期的x轴值密度

2024-06-11 21:43:44 发布

您现在位置:Python中文网/ 问答频道 /正文

一般来说,python和编程都是新手,请耐心听我说。我有一个从.csv文件导入的数据集,我试图在一年内按日期(x轴)绘制一列值(y轴),但问题是日期太密集,我一辈子都不知道如何将它们隔开或修改它们的定义。以下是我正在使用的代码:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
from scipy import stats
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt

df = pd.read_csv('Vanuatu Earthquakes 2018-2019.csv')

这是线图代码:

plt.figure(figsize=(15, 7))
ax = sns.lineplot(x='date', y='mag', data=df).set_title("Earthquake magnitude May 2018-2019")

plt.xlabel('Date')
plt.ylabel('Magnitude (Mw)')
plt.savefig('EQ mag time')

目前,这给了我一个线条图: 1

目前,我想每天打一个小勾,每周开始打一个大勾+标签。不一定是这样,但我主要是想降低密度。我在这里看过很多帖子,但是没有一篇适合我的情况,所以如果有任何帮助,我将不胜感激

[更新]

按照下面Konqui的建议得到了日期,我的代码现在如下所示:

time = pd.date_range(start = '01-05-2018',
                     end = '01-05-2019',
                     freq = 'D')
df = pd.DataFrame({'date': list(map(lambda x: str(x), time)),
                   'mag': np.random.random(len(time))})

plt.figure(figsize=(15, 7))
df['date'] = pd.to_datetime(df['date'], format = '%Y-%m')
ax = sns.lineplot(x='date', y='mag', data=df).set_title("Earthquake magnitude May 2018-2019")
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday = 1))
ax.xaxis.set_major_formatter(md.DateFormatter('%Y-%m-%d'))
plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90)
ax.xaxis.set_minor_locator(md.DayLocator(interval = 1))
plt.xlabel('Date')
plt.ylabel('Magnitude (Mw)')

这给了我一条错误消息:AttributeError: 'Text' object has no attribute 'xaxis'。有什么想法吗


Tags: csv代码importdfdatetimeasplt
1条回答
网友
1楼 · 发布于 2024-06-11 21:43:44

假设

我假设您从一个类似于保存在Vanuatu Earthquakes 2018-2019.csv文件中的数据帧开始:

import pandas as pd
import numpy as np

time = 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       mag
0  2020-01-01 00:00:00  0.940040
1  2020-01-02 00:00:00  0.765570
2  2020-01-03 00:00:00  0.951839
3  2020-01-04 00:00:00  0.708172
4  2020-01-05 00:00:00  0.705032
5  2020-01-06 00:00:00  0.857500
6  2020-01-07 00:00:00  0.866418
7  2020-01-08 00:00:00  0.363287
8  2020-01-09 00:00:00  0.289615
9  2020-01-10 00:00:00  0.741499

绘图:

import seaborn as sns
import matplotlib.pyplot as plt

fig, 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()

enter image description here


答复

你应该做一系列的事情:

  1. 首先,由于'date'值是str类型,因此需要将它们转换为datetime

    df['date'] = pd.to_datetime(df['date'], format = '%Y-%m-%d')
    

    这样,您的x轴是datetime类型,上面的图将变成:

enter image description here

  1. 然后你必须调整滴答声;对于主刻度,应设置:

    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 day
    ax.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)
    

    因此,最终的情节将是:

enter image description here


完整代码

# import required packages
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.dates as md

# read the dataframe
df = pd.read_csv('Vanuatu Earthquakes 2018-2019.csv')
# convert 'date' column type from str to datetime
df['date'] = pd.to_datetime(df['date'], format = '%Y-%m-%d')

# prepare the figure
fig, ax = plt.subplots(figsize = (15, 7))

# set up the plot
sns.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 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 day
ax.xaxis.set_minor_locator(md.DayLocator(interval = 1))

# set ticks length
ax.tick_params(axis = 'x', which = 'major', length = 10)
ax.tick_params(axis = 'x', which = 'minor', length = 5)

# set axes labels
plt.xlabel('Date')
plt.ylabel('Magnitude (Mw)')

# show the plot
plt.show()

注释

如果注意我的图中的y轴,您会看到'mag'值落在范围(0-1)内。这是因为我用'mag': np.random.random(len(time))生成了这个数据。如果从文件Vanuatu Earthquakes 2018-2019.csv读取您的数据,您将在y轴上获得正确的值。尝试简单地复制完整代码部分中的代码

相关问题 更多 >