# Plot Line Graph in Python

## What is Line Plot?

Line graph is basically a graph of a line joining the data points. Data points are also known as markers. The graph can be of any shape. For ex. straight line, curve, or any other shape. Line plot is a basic type of chart which is commonly used in many fields. To plot line graph in python using matplotlib package, use `plot()` function of matplotlib library.

In this article, I will explain you how to plot line graph in python using matplotlib package.

## Installation of Packages

We will need matplotlib packages to create line chart in python. If you don’t have these packages installed on your system, install it using below commands.

`pip install matplotlib`

## Parameters for Customization of Line Plot

```plot(x, y)        # plot x and y using default line style and color
plot(x, y, 'bo')  # plot x and y using blue circle markers
plot(y)           # plot y using x as index array 0..N-1
plot(y, 'r+')     # ditto, but with red plusses

representation formats for styling
**Markers**

=============    ===============================
character        description
=============    ===============================
``'.'``          point marker
``','``          pixel marker
``'o'``          circle marker
``'v'``          triangle down marker
``'^'``          triangle up marker
``'<'``          triangle left marker
``'>'``          triangle right marker
``'1'``          tri down marker
``'2'``          tri_up marker
``'3'``          tri_left marker
``'4'``          tri_right marker
``'s'``          square marker
``'p'``          pentagon marker
``'*'``          star marker
``'h'``          hexagon1 marker
``'H'``          hexagon2 marker
``'+'``          plus marker
``'x'``          x marker
``'D'``          diamond marker
``'d'``          thin_diamond marker
``'|'``          vline marker
``'_'``          hline marker
=============    ===============================

**Line Styles**

=============    ===============================
character        description
=============    ===============================
``'-'``          solid line style
``'--'``         dashed line style
``'-.'``         dash-dot line style
``':'``          dotted line style
=============    ===============================

Example format strings::

'b'    # blue markers with default shape
'or'   # red circles
'-g'   # green solid line
'--'   # dashed line with default color
'^k:'  # black triangle_up markers connected by a dotted line

**Colors**

The supported color abbreviations are the single letter codes

=============    ===============================
character        color
=============    ===============================
``'b'``          blue
``'g'``          green
``'r'``          red
``'c'``          cyan
``'m'``          magenta
``'y'``          yellow
``'k'``          black
``'w'``          white
=============    ===============================
We can use any combination of color, markers and styling to the graph. And make the graph customized the way we want.```

## How to Plot Line Graph in Python

Let’s see an example to plot line chart using matplotlib library

### Installation of Packages

We will need matplotlib packages to show line graph. Install packages using below command.

`pip install matplotlib`

### Import libraries

Import matplotlib libraries in our python line graph code to get started with plotting line chart.

```# Import library

import matplotlib.pyplot as plt```

Cool Tip: Learn How to plot bubble chart in python !

### Prepare dataset

Create a dataset for x and y axis to plot line chart to display air quality forecast for week

• Prepare dataset of days for x axis
• Create dataset of temperature for y axis
```# Create a dataset of air quality forecast to plot line graph
# Create dataset of days for x-axis
x=['Saturday','Sunday','Monday','Tuesday','Wednesday','Thursday','Friday']

# Create dataset of temperature for y axis
y=[30,31,31,32,33,29,29.5]```

### Draw Line chart using Plot()

Use `plt.plot() `function of matplotlib module to draw line graph.

```# Plot the Line graph
plt.plot(x,y)```

### Set Line Chart Title and Labels

Use matplotlib title and label function to assign title and label for x axis and y axis.

```# Set the title and Labels
plt.title('Line Plot for 7 day forecast')
plt.xlabel('Days')
plt.ylabel('Day Time Temperature')```

### Plot Line Graph

Use show() function of matplotlib.pyplot module to plot line graph

`plt.show()`

### Python Line Plot Code

Use below entire Line plot in python code using matplotlib library

```# Import library

import matplotlib.pyplot as plt

# Create a dataset of air quality forecast to plot line graph
# Create dataset of days for x-axis
x=['Saturday','Sunday','Monday','Tuesday','Wednesday','Thursday','Friday']

# Create dataset of temperature for y axis
y=[30,31,31,32,33,29,29.5]

# Plot the Line graph
plt.plot(x,y)

# Set the title and Labels
plt.title('Line Plot for 7 day forecast')
plt.xlabel('Days')
plt.ylabel('Day Time Temperature')

# Show the Line Graph

plt.show()
```

### Line Graph Output

Cool Tip: Learn How to plot basic area plot in python !

## Create Line Plot from list Python

Let’s see an example to plot line chart from list in Python.

### Installation of Packages

We will require matplotlib.pyplot packages installed on our system to draw line plot. If we don’t have packages installed, we can install packages with the command below.

`pip install matplotlib`

### Import libraries

We will import matplotlib.pyplot libraries to plot line chart.

`import matplotlib.pyplot as plt`

### Prepare dataset and Plot the graph

• The first argument defines list for x axis
• The second argument defines list for y axis
```# Pass the lists directly to the plot function
# make a list of numbers for x_axis dataset
# make a list of cubes of numbers for y_axis dataset
# Plot the graph
plt.plot([1,2,3,4],[1,8,27,64])```

### Set Line Chart Title and Labels

Use matplotlib title and label function to assign title and label for x axis and y axis.

```# Set the title and Labels for Line Chart
plt.title('Line plot for cubes of numbers')
plt.xlabel('Numbers')
plt.ylabel('Cubes')```

### Show the Line Chart

Use show() function of matplotlib.pyplot module to plot line graph

`plt.show()`

### Line Chart Python Code

Use below Line chart in python matplotlib source code

```# Import library

import matplotlib.pyplot as plt

# Pass the lists directly to the plot function
# make a list of numbers for x_axis dataset
# make a list of cubes of numbers for y_axis dataset
# Plot the graph

plt.plot([1,2,3,4],[1,8,27,64])

# Set the title and Labels for Line Chart
plt.title('Line plot for cubes of numbers')
plt.xlabel('Numbers')
plt.ylabel('Cubes')

plt.show()```

### Line Chart output

Cool Tip: Learn How to plot Treemap Chart in python !

## Python Line Plot with Dots

In this matplotlib line chart with python example, I will explain you to customize line chart in python to display blue color dotted line and display points as dots.

### Installation of Packages

Follow above givens steps to install required packages and import libraries to get started with plotting line chart in python.

```# Import libraries
import matplotlib.pyplot as plt
import numpy as np```

### Prepare Dataset

We will create python line plot with dots and blue color line.

• Create dataset for x variable using numpy arange() function
• Create dataset for y variable using x dataset values
```#create a VARIABLES (non int) and pass the functions of variables to plot

# Create x_axis dataset
x=np.arange(1,5)

# Create y_axis dataset
y=x**5```

### Plot Line Chart with dots in Python

Use plot() function of matplotlib module to plot Line chart.

• The first argument define x axis dataset
• The second argument define y axis dataset
• The third argument s defines linestyle as dotted line
• The fourth argument defines marker as ‘o’ for dot
• The fifth argument defines color as blue
```# Draw line plot with dots
plt.plot(x+10,y-15,linestyle='--', marker='o', color='b')```

### Show Line Chart

Using  Matplotlib show() function to show the graphical view of Line chart.

`plt.show()`

### Line Chart Python Code

Use below python line plot with dots source code

```# Import libraries
import matplotlib.pyplot as plt
import numpy as np

# Create x_axis dataset
x=np.arange(1,5)

# Create y_axis dataset
y=x**5

# Draw line plot with dots
plt.plot(x+10,y-15,linestyle='--', marker='o', color='b')

# Show the Line graph with dots
plt.show()
```

### Line Chart output

Use `plt.plt()` function of matplotlib module to create line chart.
Use `linestyle and color` argument in plot() function of matplotlib library to plot line chart with dots and color dotted line.