对python和一般编程来说都是很新的,所以请耐心听我说。我有一个从.csv文件导入的数据集,我试图在1年的时间内按日期(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')
下面是line plot代码:
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')
这给了我这个线图:
目前,我想通过每天一个小的tick和每周开始时一个更大的tick +标签来完成。不一定要完全那样,但我主要是想降低密度。我看了很多帖子,但似乎没有一个适合我的情况,所以任何帮助都会非常感谢。
按照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'
有什么想法吗
1条答案
按热度按时间e0bqpujr1#
假设
我假设你从一个类似于保存在
Vanuatu Earthquakes 2018-2019.csv
文件中的框架开始:产出:
绘图:
回复
你应该做一系列的事情:
1.首先,由于
'date'
值是str
类型,因此您需要通过以下方式将其转换为datetime
,从而获得该标签密度这样你的x轴是
datetime
类型,上面的图将变成这样:1.然后你必须调整滴答声;对于您应该设置的主要刻度:
对于次要蜱虫:
也可以使用以下命令编辑刻度的长度:
所以最后的图将变成:
完整代码
备注
如果你注意我的图中的y轴,你会看到
'mag'
的值落在(0-1)
的范围内。这是因为我用'mag': np.random.random(len(time))
生成了这个 * 假 * 数据。如果你从Vanuatu Earthquakes 2018-2019.csv
文件中读取你的数据,你将在y轴上得到正确的值。请尝试简单复制完整代码部分的代码。