In Python, the pyplot library of the Matplotlib module helps in achieving data visualization through easy ways. We can create different graphs, but in this article, we will be discussing the Line graph. We will be using the ‘plot’ method to view our data in a graphical representation.
pyplot.plot() syntax
Let us look at the plot function arguments,
plt.plot(x,y, scalex=True, scaley=True, data=None, **kwargs)
Parameters
 x, y : These can be arrays or any arraylike object.
 They represents the horizontal/vertical coordinates of the data points.
 data : indexable object, optional. An object with labeled data.
 If provided, then will be used as the label names to plot in *x* and *y*.
 scalex, scaley : bool, default: True
 Optional parameters.
 These parameters determines if the view limits are adapted to the data limits or not.
 The values are passed on to `autoscale_view`.
Returns
 A list of Line2D objects, which represents the plotted data.
Matplotlib – Line Plot Examples
Example 1: plotting two lists
Let us start with a simple example where we have two arrays x and y, which we will be plotting on the graph,
import matplotlib.pyplot as plt x= [1,2,3,4] y=[2,4,6,8] plt.plot(x,y) plt.show()
Output:
Let us look at another example,
Example 2: plotting two numpy arrays
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,5,100) y = np.exp(x) plt.plot(x, y) plt.show()
Output
Add Titles and labels in the line chart using matplotlib
Now that we have learned to plot our data let us add titles and labels to represent our data in a better manner.
We will be using title() method to give a heading to the graph we have created in the previous example and label() method to define our x and y axis.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,5,100) y = np.exp(x) plt.plot(x, y) plt.title('e^x') plt.show()
Output:
Now we can quickly identify that the graph we have created is of the function e^x. Let us add the labels to x and y axes.
plt.xlabel('X Values') plt.ylabel('Y Values') plt.show()
Output:
Matplotlib: Plot lines from numpy array
We can create a numpy array and pass the same in the plot method.
Here we have created a numpy array using the arrange() method. This will provide values from 5 to 20 with a step size of 0.5. The array is then passed into the square function to obtain y values.
import matplotlib.pyplot as plt import numpy as np x = np.arange(5,20,.5) y = np.square(x) plt.plot(x,y) plt.title('Square Function') plt.xlabel('x values') plt.ylabel('y= x^2') plt.show()
Output:
Plot lines from Dataframe in Matplotlib
We can even create a dataframe and use the data to create our plot. We will be looking at 2 different methods to achieve the same output
We have created a dataframe with years of work experience, and the salary received. Here we will fetch years and salary data and assign to x and y variables and then we can simply use the plot() method from matplotlib to plot our data.
from pandas import DataFrame import matplotlib.pyplot as plt import numpy as np Data = {'Experience': np.arange(2,30,2), 'Salary': [10000,15000,20000,30000, 35000,40000,42000,44000, 46000,48000,50000,51000, 52000,53000] } df = DataFrame(Data,columns=['Experience','Salary']) x=df.iloc[:,0] y=df.iloc[:,1] plt.title('Experience vs Salary') plt.xlabel('Experience in Years') plt.ylabel('Salary in Dollars') plt.plot(x,y) plt.show()
Output:
There is another way of achieving the same. We can call the plot function from dataframe to plot our data.
Datarframe.plot()
This method is used to make plots of Series or DataFrame. The plot method on Series and DataFrame is just a simple wrapper around plt.plot. It will take column names as labels
Let us look at the arguments
df.plot(data, x, y, kind)
Parameters
 x : label or position, default None
 It will be only used, if df is a DataFrame object.
 y : The label or position or list of label, positions.
 Default value is None
 Allows plotting of one column versus another. Only used if data is a DataFrame.
 kind : str
 The kind of plot to produce
Returns
 :class:`matplotlib.axes.Axes` or numpy.ndarray
Let’s see an example,
from pandas import DataFrame import matplotlib.pyplot as plt import numpy as np Data = {'Experience': np.arange(2, 30, 2), 'Salary': [10000, 15000, 20000, 30000, 35000, 40000, 42000, 44000, 46000, 48000, 50000, 51000, 52000, 53000] } df = DataFrame(Data, columns=['Experience', 'Salary']) df.plot(x='Experience', y='Salary', kind='line', title='Experience vs Salary')
Output:
Create timeseries graph using dataframe and plot method
Firstly we have created a DataFrame with values of Profit ranging from 100% to 100 % for a time starting from January 2005.
import pandas as pd import numpy as np from pandas import DataFrame Data = {'Profit':np.random.randint(100,100,size=200), 'Time Period': pd.date_range('1/1/2005', periods=200) } df = DataFrame(Data,columns=['Profit','Time Period']) df.set_index('Time Period', inplace=True) print(df)
Output:
Profit Time Period 20050101 46 20050102 74 20050103 68 20050104 78 20050105 93 ... ... 20050715 71 20050716 71 20050717 21 20050718 1 20050719 95 [200 rows x 1 columns]
Our dataframe has 200 rows and 1 column. Please note that the values you obtain will be different from the data displayed above,since we are using the random function.
Now we’ll find the cumulative value of the profit percentage
df=df.cumsum() print(df)
Output
Profit Time Period 20050101 46 20050102 28 20050103 96 20050104 174 20050105 81 ... ... 20050715 646 20050716 717 20050717 738 20050718 737 20050719 832 [200 rows x 1 columns]
Let’s plot this series,
df.plot()
Output:
Summary
In this article, we have learnt the usage of the plot method from Matplotlib.pyplot library. We have looked into different examples through which we can plot lines. We also discussed adding labels and titles to our x,y graph to make it more readable. We then took a deeper dive and looked at a few examples to plot the data from Dataframe using two techniques.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.