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()
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)
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)
Hi,
Could you let me know what is “extra parnens”?
He means the “missing parentheses” (not really ‘extra’).
this code sucks
Nice code! But we can always work on customizing and beautifying the plots
Hello Sir,
Can we reposition the labels of multiple lines?
In my graph, the labels and lines are overlapping. Thus, I need to change the position of labels for easy understanding of the graph.
Thanks
Taruchit
My system seems to gag on “import matplotlib.pyplot as plt
It reports ModuleNotFoundError: No module named ‘matplotlib’port matplotlib.pyplot as plt”
Is there software I need to install?