0

I have data on measles cases obtained from this histogram. I would like to recreate this plot for a paper I am writing.

I load the data using 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()

This doesn't produce an x axis formatted like the picture linked.

I've also tried something similar to

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

But I can then not distinguish when the months begin and end very easily. Can someone recommend a way to format the x-axis similar to the picture I have linked?

Demetri Pananos
  • 6,770
  • 9
  • 42
  • 73
  • Looks like a duplicate of [this question](http://stackoverflow.com/questions/19184484) – Primer Jan 26 '16 at 11:11
  • 1
    Possible duplicate of [How to add group labels for bar charts in matplotlib?](http://stackoverflow.com/questions/19184484/how-to-add-group-labels-for-bar-charts-in-matplotlib) – Paul Jan 26 '16 at 23:35

1 Answers1

0

interesting problem.

I really dont know any command or setting that can do such a thing. So I used latex inside matplotlib as a textbox, that way I can place it wherever I want.

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

And output is :

enter image description here

I'm not sure how to do " January, February, ... " subsections within xlabel. If it's that important, you can add it later with photoshop :D Hope this helps!

VlS
  • 586
  • 3
  • 13