#122 Multiple lines chart

Graphics #120 and #121 show you how to create a basic line chart and how to apply basic customization. This posts explains how to make a line chart with several lines. Each line represents a set of values, for example one set per group. To make so with matplotlib we just have to call the plot function several times (one time per group).


# libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# Data
df=pd.DataFrame({'x': range(1,11), 'y1': np.random.randn(10), 'y2': np.random.randn(10)+range(1,11), 'y3': np.random.randn(10)+range(11,21) })

# multiple line plot
plt.plot( 'x', 'y1', data=df, marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4)
plt.plot( 'x', 'y2', data=df, marker='', color='olive', linewidth=2)
plt.plot( 'x', 'y3', data=df, marker='', color='olive', linewidth=2, linestyle='dashed', label="toto")
plt.legend()

2 comments

  • Does this not with with python 3? I am getting an error with copy and pasted code to be sure I wasn’t messing something up.
    RuntimeWarning: Second argument ‘y1’ is ambiguous: could be a color spec but is in data. Using as data.
    Either rename the entry in data or use three arguments to plot.
    ret = ax.plot(*args, **kwargs)

    Reply
    • Discovered that using extra parnens gets rid of the error
      plt.plot((‘x’, ‘y1′), data=df, marker=’o’, markerfacecolor=’blue’, markersize=12, color=’skyblue’, linewidth=4)

      Reply

Leave a Reply

Your email address will not be published.