0% found this document useful (0 votes)
59 views8 pages

Matplotlib

The document contains code for creating various matplotlib plots using NumPy and pandas. It includes code to generate scatter plots, bar charts, histograms, box plots, and time series plots from sample data. The plots demonstrate different plotting styles and options in matplotlib.

Uploaded by

Mahevish Fatima
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)
59 views8 pages

Matplotlib

The document contains code for creating various matplotlib plots using NumPy and pandas. It includes code to generate scatter plots, bar charts, histograms, box plots, and time series plots from sample data. The plots demonstrate different plotting styles and options in matplotlib.

Uploaded by

Mahevish Fatima
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/ 8

3/16/23, 7:34 PM Untitled42 - Jupyter Notebook

In [1]:

import numpy as np
import [Link] as plt
[Link]('seaborn-whitegrid')

In [2]:

fig, ax = [Link]()
print(type(fig))
print(type(ax))

<class '[Link]'>
<class '[Link]._subplots.AxesSubplot'>

In [3]:

fig, ax = [Link]()
x = [Link](1,10, size=10)
y = 2*x
[Link](x,y) # same as [Link](x,y)
[Link]()

localhost:8888/notebooks/[Link]?kernel_name=python3 1/8
3/16/23, 7:34 PM Untitled42 - Jupyter Notebook

In [4]:

fig, ax = [Link]()
x = [Link](0,10,1000)
y = 2*x

# set of default color and style


[Link](x, [Link](x))
# RGB tuple, values 0 to 1, solid style
[Link](x, [Link](x), color=(1.0,0.2,0.3),linestyle='-')
# specify color by name, dashed style
[Link](x, y, color='blue', linestyle='--')
# short color code (rgbcmyk), dotted style
[Link](x, x+3, color='g',linestyle=':')
# Grayscale between 0 and 1, dashdot style
[Link](x, [Link](x+1), color='0.75',linestyle='-.')
# Hex code (RRGGBB from 00 to FF), dashed style
[Link](x, x, color='#FFDD44',linestyle='--')

Out[4]:

[<[Link].Line2D at 0x21bc069f970>]

localhost:8888/notebooks/[Link]?kernel_name=python3 2/8
3/16/23, 7:34 PM Untitled42 - Jupyter Notebook

In [5]:

[Link](x, [Link](x), label='y=sin(x)')


[Link](x,[Link](x), label='y=cos(x)')
[Link]('Sine and Cosine Functions ')
[Link]("x-axis")
[Link]("y-axis")
[Link]()# Describing the element of the graph
[Link]()

Scatter Plot
In [6]:

import pandas as pd
df=pd.read_csv("happiness_Rank.csv")

In [7]:

[Link]()

Out[7]:

Country Rank Score Support GDP Health Freedom Generosity Corruption Y

0 Switzerland 1 7.587 1.39651 1.34951 0.94143 0.66557 0.41978 0.29678 2

1 Iceland 2 7.561 1.30232 1.40223 0.94784 0.62877 0.14145 0.43630 2

2 Denmark 3 7.527 1.32548 1.36058 0.87464 0.64938 0.48357 0.34139 2

3 Norway 4 7.522 1.45900 1.33095 0.88521 0.66973 0.36503 0.34699 2

4 Canada 5 7.427 1.32629 1.32261 0.90563 0.63297 0.32957 0.45811 2

localhost:8888/notebooks/[Link]?kernel_name=python3 3/8
3/16/23, 7:34 PM Untitled42 - Jupyter Notebook

In [8]:

fig, ax = [Link](figsize = (12,6))


x = df['GDP']
y = df['Score']
[Link](x,y)
[Link]('GDP vs Happiness Score')
[Link]('GDP')
[Link]('Score')

Out[8]:

Text(0, 0.5, 'Score')

BarChart
In [10]:

df_19=df[df["Year"]==2019]
df_19.head()

Out[10]:

Country Rank Score Support GDP Health Freedom Generosity Corruption Ye

626 Finland 1 7.769 1.587 1.340 0.986 0.596 0.153 0.393 20

627 Denmark 2 7.600 1.573 1.383 0.996 0.592 0.252 0.410 20

628 Norway 3 7.554 1.582 1.488 1.028 0.603 0.271 0.341 20

629 Iceland 4 7.494 1.624 1.380 1.026 0.591 0.354 0.118 20

630 Netherlands 5 7.488 1.522 1.396 0.999 0.557 0.322 0.298 20

localhost:8888/notebooks/[Link]?kernel_name=python3 4/8
3/16/23, 7:34 PM Untitled42 - Jupyter Notebook

In [11]:

countries = ['United States','Japan', 'Germany','Brazil', 'India']


y_pos = [Link](len(countries))
data = df_19[(df_19['Country'].isin(countries))].sort_values(['Country'])
data.sort_values('GDP', inplace=True)
data.reset_index(drop=True)
[Link](y_pos, data['GDP'], align='center', alpha=0.5)
[Link](y_pos, data['Country'])
[Link]('GDP')
[Link]('Bar Chart')
[Link]()

Histogram

localhost:8888/notebooks/[Link]?kernel_name=python3 5/8
3/16/23, 7:34 PM Untitled42 - Jupyter Notebook

In [12]:

fig = [Link](figsize=(8,6))
[Link](df_19['Corruption'], bins=6, density=True)
[Link](alpha=0.2)
[Link]()

Box Plot

localhost:8888/notebooks/[Link]?kernel_name=python3 6/8
3/16/23, 7:34 PM Untitled42 - Jupyter Notebook

In [13]:

# Create dataset
user_1 = [10, 3, 15, 21, 17, 14]
user_2 = [5, 13, 10, 7, 9, 12]
data = [user_1, user_2]
fig = [Link](figsize =(8, 6))

# Create axes instance


ax = fig.add_axes([0, 0, 1, 1])

# Create plot
bp = [Link](data)

# Show plot
[Link]([1,2],['user_1','user_2'])
[Link]()

Time Series Plot

localhost:8888/notebooks/[Link]?kernel_name=python3 7/8
3/16/23, 7:34 PM Untitled42 - Jupyter Notebook

In [14]:

# Time Series Plot


[Link](figsize=(8,6))
ts = [Link]([Link](100), index = pd.date_range(
'1/1/2020', periods = 100))
# Return the cumulative sum of the elements.
ts = [Link]()
[Link]()
[Link]()

In [ ]:

localhost:8888/notebooks/[Link]?kernel_name=python3 8/8

You might also like