1

I am trying to generate a smooth line using a dataset that contains time (measured as number of days) and a set of numbers that represent a socioeconomic variable.

Here is a sample of my data:

date, data
726,1.2414
727,1.2414
728,1.2414
729,1.2414
730,1.2414
731,1.2414
732,1.2414
733,1.2414
734,1.2414
735,1.2414
736,1.2414
737,1.804597701
738,1.804597701
739,1.804597701
740,1.804597701
741,1.804597701
742,1.804597701
743,1.804597701
744,1.804597701
745,1.804597701
746,1.804597701
747,1.804597701
748,1.804597701
749,1.804597701
750,1.804597701
751,1.804597701
752,1.793103448
753,1.793103448
754,1.793103448
755,1.793103448
756,1.793103448
757,1.793103448
758,1.793103448
759,1.793103448
760,1.793103448
761,1.793103448
762,1.793103448
763,1.793103448
764,1
765,1 

This is my code so far:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline

out_file = "path_to_file/file.csv"
df = pd.read_csv(out_file)

time = df['date']
data = df['data']
ax1 = plt.subplot2grid((4,3),(0,0), colspan = 2, rowspan = 2) # Will be adding other plots
plt.plot(time, data)
plt.yticks(np.arange(1,5,1)) # Include classes 1-4 showing only 1 step changes
plt.gca().invert_yaxis() # Reverse y axis 
plt.ylabel('Trend', fontsize = 8, labelpad = 10)

This generates the following plot:

Test plot

I have seen posts that answer similar questions (like the ones below), but can't seem to get my code to work. Can anyone suggest an elegant solution?

Generating smooth line graph using matplotlib

Python Matplotlib - Smooth plot line for x-axis with date values

3
  • 2
    Welcome to stack overflow. Could you please include an example dataset in your post so other users can reproduce your problem. Commented Feb 13, 2019 at 19:55
  • @jwalton3141, thanks! I've added a sample of the data. Commented Feb 13, 2019 at 21:06
  • 1
    One of the answers uses e.g. spline. Did you try that? What happens? Commented Feb 13, 2019 at 21:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.