0% found this document useful (0 votes)
10 views6 pages

36 Linecharts

The document provides an overview of creating line charts using the Matplotlib library in Python, explaining the basic concepts and functionalities. It includes examples of simple line plots, adding labels, displaying multiple charts, and customizing line styles, colors, and widths. Additionally, it demonstrates how to fill the area between two plots and plot multiple lines on the same graph.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

36 Linecharts

The document provides an overview of creating line charts using the Matplotlib library in Python, explaining the basic concepts and functionalities. It includes examples of simple line plots, adding labels, displaying multiple charts, and customizing line styles, colors, and widths. Additionally, it demonstrates how to fill the area between two plots and plot multiple lines on the same graph.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Line Charts

Matplotlib is a data visualization library in Python. The pyplot, a sublibrary


of matplotlib, is a collection of functions that helps in creating a variety of
charts. Line charts are used to represent the relation between two data X
and Y on a different axis. Here we will see some of the examples of a line
chart in Python :

What Is a Line Chart?


A line chart is a graphical representation of an asset's historical price
action that connects a series of data points with a continuous line. This
is the most basic type of chart used in finance, and it typically only
depicts a security's closing prices over time

Simple line plots

First import Matplotlib.pyplot library for plotting functions. Also, import the
Numpy library as per requirement. Then define data values x and y.

# importing the required libraries


import matplotlib.pyplot as plt
import numpy as np

# define data values


x = np.array([1, 2, 3, 4]) # X-axis points
y = x*2 # Y-axis points

plt.plot(x, y) # Plot the chart


plt.show() # display

we can see in the above output image that there is no label on the x-axis
and y-axis. Since labeling is necessary for understanding the chart
dimensions. In the following example, we will see how to add labels, Ident
in the charts

import matplotlib.pyplot as plt


import numpy as np

# Define X and Y variable data


x = np.array([1, 2, 3, 4])
y = x*2

plt.plot(x, y)
plt.xlabel("X-axis") # add X-axis label
plt.ylabel("Y-axis") # add Y-axis label
plt.title("Any suitable title") # add title
plt.show()

Multiple charts:
We can display more than one chart in the same container by
using pyplot.figure() function. This will help us in comparing the different
charts and also control the look and feel of charts .

import matplotlib.pyplot as plt


import numpy as np

x = np.array([1, 2, 3, 4])
y = x*2

plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Any suitable title")
plt.show() # show first chart

# The figure() function helps in creating a


# new figure that can hold a new chart in it.
plt.figure()
x1 = [2, 4, 6, 8]
y1 = [3, 5, 7, 9]
plt.plot(x1, y1, '-.')

# Show another chart with '-' dotted line


plt.show()

Multiple plots on the same axis:

import matplotlib.pyplot as plt


import numpy as np

x = np.array([1, 2, 3, 4])
y = x*2

# first plot with X and Y data


plt.plot(x, y)

x1 = [2, 4, 6, 8]
y1 = [3, 5, 7, 9]

# second plot with x1 and y1 data


plt.plot(x1, y1, '-.')
plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.title('multiple plots')
plt.show()

Fill the area between two plots


Using the pyplot.fill_between() function we can fill in the region
between two line plots in the same graph. This will help us in
understanding the margin of data between two line plots based on certain
conditions.

import matplotlib.pyplot as plt


import numpy as np

x = np.array([1, 2, 3, 4])
y = x*2

plt.plot(x, y)

x1 = [2, 4, 6, 8]
y1 = [3, 5, 7, 9]

plt.plot(x, y1, '-.')


plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.title('multiple plots')

plt.fill_between(x, y, y1,
color='green', alpha=0.5)
plt.show()
Some more Examples:

Linestyle
You can use the keyword argument linestyle, or shorter ls, to change the style of
the plotted line:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linestyle = 'dotted') #dashed


plt.show()

The line style can be written in a shorter syntax:


linestyle can be written as ls.
dotted can be written as :.
dashed can be written as --

Line Color
You can use the keyword argument color or the shorter c to set the color of the line:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])


plt.plot(ypoints, color = 'r')
plt.show()

Line Width
You can use the keyword argument linewidth or the shorter lw to change the width
of the line.

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linewidth = '20.5')


plt.show()

Multiple Lines:
import matplotlib.pyplot as plt
import numpy as np

x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])

plt.plot(x1, y1, x2, y2)


plt.show()

You might also like