0% found this document useful (0 votes)
26 views4 pages

4.2 2. Line Chart Plot

This document discusses how to create line charts in Python using Matplotlib. It covers styling charts, adding labels, legends and grids, creating multiple line charts on the same axes, and formatting and saving line charts.

Uploaded by

john ra
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)
26 views4 pages

4.2 2. Line Chart Plot

This document discusses how to create line charts in Python using Matplotlib. It covers styling charts, adding labels, legends and grids, creating multiple line charts on the same axes, and formatting and saving line charts.

Uploaded by

john ra
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/ 4

1.

Line Chart
import matplotlib.pyplot as plt

import pandas as pd
import numpy as np

Chart Styling
plt.style.available # List of styling options

['Solarize_Light2',
'_classic_test_patch',
'_mpl-gallery',
'_mpl-gallery-nogrid',
'bmh',
'classic',
'dark_background',
'fast',
'fivethirtyeight',
'ggplot',
'grayscale',
'seaborn',
'seaborn-bright',
'seaborn-colorblind',
'seaborn-dark',
'seaborn-dark-palette',
'seaborn-darkgrid',
'seaborn-deep',
'seaborn-muted',
'seaborn-notebook',
'seaborn-paper',
'seaborn-pastel',
'seaborn-poster',
'seaborn-talk',
'seaborn-ticks',
'seaborn-white',
'seaborn-whitegrid',
'tableau-colorblind10']

plt.style.use('classic') # Selecting a Chart style

Example of a line Chart


Years= [0,1,2,3,4,5] # Years of Experience

Mumbai_Salary=[5,6.4,7.2,8.5,10,13.7] # Salary for Engineers in Mumbai

Bangalore_Salary=[3,4,5.2,6.5,9,11.7] # Salary for Engineers in Bengaluru

plt.plot (Mumbai_Salary)

plt.xlabel("X-Axis- Experience (in Years)") # Adds an X-Label


plt.ylabel("Y-Axis- Salary growth (in Lakhs)") # Adds a Y-Label
plt.title("Years Vs Salary graph") # Adds a title
plt.show() # Displays the graph
Legend of a Graph
plt.plot(Years,Mumbai_Salary,label='Mumbai Salary')

plt.xlabel("X-Axis- Experience (in Years)") # Adds an X-Label


plt.ylabel("Y-Axis- Salary growth (in Lakhs)") # Adds a Y-Label
plt.title("Years Vs Salary graph") # Adds a title

plt.grid() # Adds a grid


plt.legend() # Adds a Legend
plt.show() # Displays the graph

# To change the Location of the chart use "Loc="

# 'best' 0, 'upper right' 1


# 'upper Left' 2, 'Lower Left' 3

# 'Lower right' 4, 'right' 5


# 'center left' 6, 'center right' 7
# 'Lower center' 8, 'upper center' 9
# 'center' 10

Multiple line Chart


plt.plot(Years, Mumbai_Salary, label='Mumbai Salary')
plt.plot(Years, Bangalore_Salary,label='Bangalore Salary')

plt.xlabel("X-Axis- Experience (in Years)") # Adds an X-Label


plt.ylabel("Y-Axis- Salary growth (in Lakhs)") # Adds a Y-Label
plt.title("Years Vs Salary graph") # Adds a title
plt.grid() # Adds a grid
plt.legend() # Adds a Legend
plt.show() # Displays the graph

Formating and Saving a Line Chart


plt.plot(Years, Mumbai_Salary, color="#ff8000",marker='8',linestyle='--',label='Mumbai Salary')

plt.plot(Years, Bangalore_Salary, color="#0000e6",marker='s',linestyle='-.',label='Bangalore Salary')

plt.xlabel("X-Axis- Experience (in Years)") # Adds an X-Label


plt.ylabel("Y-Axis- Salary growth (in Lakhs)") # Adds a Y-Label
plt.title("Years Vs Salary graph") # Adds a title

plt.grid() # Adds a grid


plt.legend() # Adds a Legend
plt.savefig('fig_name.png') # Saving the chart
plt.show() # Displays the graph

# HexCode for Selecting Colours

#ff8000 -Orange
#ffffee -Yellow
#ff0000 -Red
#0000e6 -Blue
#00cc00 -Green
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

You might also like