0

I am trying to make a plot using matplotlib. My current dates are actually ints, 195003, 195006, etc. Therefore, when I make the plot, the line chart is not smooth as there is a big gap betweem 195012 to 195101. Is there a way to solve this? Thanks a lot!

import matplotlib.pyplot as plt


x = [195003,195006,195009,195012,195103,195106,195109]

y = [1,2,3,4,3,2,1]

plt.plot(x,y)


 #This is the target - a smooth line chart

plt.figure(2)

plt.plot(y)
MB-F
  • 22,770
  • 4
  • 61
  • 116
Hansen
  • 1
  • A potential duplicate of http://stackoverflow.com/questions/5283649/plot-smooth-line-with-pyplot – Longwen Ou Feb 08 '17 at 15:31
  • Can you be more specific in what result you expect? Currently it seems you already solved the problem in figure 2. – MB-F Feb 08 '17 at 15:34
  • @LongwenOu I don't think this is a duplicate of a smoothing question. Instaed, I guess it is a duplicate of an axes labeling question :) But let's wait for the OP to clarify... – MB-F Feb 08 '17 at 15:35
  • Thanks everyone! Apologize if this is a duplicated question.. – Hansen Feb 08 '17 at 15:57
  • What I wanted was dates spaced out evenly. If the dates are int, 195012, 195103... the x axis are not evenly spaced. Thanks to the answer below - I changed int to datetime format and it solved the problem. Thanks everyone! – Hansen Feb 08 '17 at 16:01

1 Answers1

0

If I am reading this correctly, the integer 195003 represents March, 1950? So I think converting your integer dates into datetime.date objects will do what you want, as matplotlib can take a list of dates for your x data.

import datetime
x = [195003,195006,195009,195012,195103,195106,195109]
y = [1,2,3,4,3,2,1]
years = [str(y)[:4] for y in x]
months = [str(m)[4:] for m in x]
dates = [datetime.date(int(y), int(m), 1) for y,m in zip(years, months)]
plt.plot(dates, y)
A. Entuluva
  • 719
  • 5
  • 9