如何设置x轴的格式,使其看起来与此绘图类似?

2024-04-20 09:52:41 发布

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

我有从this直方图获得的麻疹病例数据。我想为我正在写的一篇论文重现这个情节。你知道吗

我使用pandas加载数据。你知道吗

import pandas as pd
import matplotlib.pyplot as plt    
df = pd.read_csv('measles_data.csv')
df['Date'] = df['Date'].apply( lambda x: datetime.datetime.strptime( j, "%Y-%m-%d")
df.set_index('Date', inplace = True)
df.hist()

plt.show()

这不会产生像链接的图片那样的x轴格式。你知道吗

我也试过类似的方法

x = [j.day() for j in df.Date]
y = df['Confirmed Cases'].as_matrix()
ax.bar(x,y)
ax.xaxis_date()

但是我很难区分这几个月的开始和结束。有人能推荐一种方法来格式化x轴类似的图片我已经链接?你知道吗


Tags: csv数据方法importpandasdfdatetimedate
1条回答
网友
1楼 · 发布于 2024-04-20 09:52:41

有趣的问题。你知道吗

我真的不知道任何命令或设置,可以做这样的事情。所以我在matplotlib中使用了乳胶作为文本框,这样我就可以把它放在任何我想放的地方。你知道吗

import datetime
import random
import matplotlib.pyplot as plt

from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)

date1 = datetime.date(2003, 1, 5)
date2 = datetime.date(2003, 3, 1)

x = [date1, date2]
y = [i+random.gauss(0,1) for i,_ in enumerate(x)]

fig, ax = plt.subplots(1)

textstr = r"$\overbrace{}^{Sometext}"

ax.text(0.1, 0.1, textstr, transform=ax.transAxes, fontsize=20,
        verticalalignment='top')

plt.plot(x, y)
plt.gcf().autofmt_xdate()

plt.show()

输出为:

enter image description here

我不知道怎么做“一月,二月,…”xlabel中的小节。如果有那么重要,你可以稍后用photoshop添加:D希望这有帮助!你知道吗

相关问题 更多 >