Search code examples
pythonmatplotlibplotline-plot

How to plot one figure with multiple lines in python using


This should be a very simple question for someone out there. I need a line plot in python where the independent variable (x-axis) is the Date. The y-axis is the dependent Value data and there will be multiple lines: one line per Name which describes the changes in Value over time. I'm not sure how to do this other than to use matplotlib.

This is how I have my data organized in df which pulled data out of a csv file.

Name = df['Name']
Value = df['expected harvest (Value)']
Date = df['Date']
result = pd.concat([Name, Value, Date], axis=1)

>>> result
                       Name                     Value      Date
1                       189                       9.0  11/14/15
2                       191                      10.0  11/14/15
3                       192                       1.0  11/14/15
4                       193                       4.0  11/14/15
...                     ...                       ...       ...
2948                    189                       7.0   2/20/16
2950                    190                       1.0   2/20/16
2952                    191                       3.0   2/20/16
2953                    192                       3.0   2/20/16
2954                    193                       0.0   2/20/16

So far I've tried this, but I need to lines to be horizontal instead of vertical and for there to be separate lines for each Name. Somehow I am missing how to group the data by Name and then plot as separate lines.

fig = plt.figure()
ax = fig.add_subplot(111)
x_points = df['Date']
x_points = pd.to_datetime(x_points)
y_points = df['expected harvest (Value)']

p = ax.plot(x_points, y_points, 'b')
ax.set_xlabel('x-points')
ax.set_ylabel('y-points')
ax.set_title('Simple XY point plot')
fig.show()

This is the wrong plot and not what I need


Solution

  • First we create sample data

    sample_dates = ['1/1/15', '1/2/15', '1/3/15', '1/4/15']
    dates = []
    for date in sample_dates:
        [dates.append(date) for _ in range(4)]
    values = np.random.randint(1, 8, size=15)
    
    names = [1, 2, 3, 4] * 4
    

    We remove some in the middle (as in the example data that 190 is not). And we transformed it into a Dataframe:

    names.pop(6)
    dates.pop(6)
    
    x_date = pd.to_datetime(dates)
    df = pd.DataFrame()
    df['names'] = names
    df['values'] = values
    df['dates'] = x_date
    

    Now we walk them by name, and we plot them

    for i in df.names.unique():
        x = df[df.names==i]['dates']
        y = df[df.names==i]['values']
        plt.plot(x, y)
    plt.show()
    

    example