Matplotlib

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

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

In [1]:

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')

In [2]:

fig, ax = plt.subplots()
print(type(fig))
print(type(ax))

<class 'matplotlib.figure.Figure'>
<class 'matplotlib.axes._subplots.AxesSubplot'>

In [3]:

fig, ax = plt.subplots()
x = np.random.randint(1,10, size=10)
y = 2*x
plt.plot(x,y) # same as ax.plot(x,y)
plt.show()

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

In [4]:

fig, ax = plt.subplots()
x = np.linspace(0,10,1000)
y = 2*x

# set of default color and style


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

Out[4]:

[<matplotlib.lines.Line2D at 0x21bc069f970>]

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

In [5]:

plt.plot(x, np.sin(x), label='y=sin(x)')


plt.plot(x,np.cos(x), label='y=cos(x)')
plt.title('Sine and Cosine Functions ')
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.legend()# Describing the element of the graph
plt.show()

Scatter Plot
In [6]:

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

In [7]:

df.head()

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/Untitled42.ipynb?kernel_name=python3 3/8
3/16/23, 7:34 PM Untitled42 - Jupyter Notebook

In [8]:

fig, ax = plt.subplots(figsize = (12,6))


x = df['GDP']
y = df['Score']
plt.scatter(x,y)
plt.title('GDP vs Happiness Score')
plt.xlabel('GDP')
plt.ylabel('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/Untitled42.ipynb?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 = np.arange(len(countries))
data = df_19[(df_19['Country'].isin(countries))].sort_values(['Country'])
data.sort_values('GDP', inplace=True)
data.reset_index(drop=True)
plt.bar(y_pos, data['GDP'], align='center', alpha=0.5)
plt.xticks(y_pos, data['Country'])
plt.ylabel('GDP')
plt.title('Bar Chart')
plt.show()

Histogram

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

In [12]:

fig = plt.figure(figsize=(8,6))
plt.hist(df_19['Corruption'], bins=6, density=True)
plt.grid(alpha=0.2)
plt.show()

Box Plot

localhost:8888/notebooks/Untitled42.ipynb?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 = plt.figure(figsize =(8, 6))

# Create axes instance


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

# Create plot
bp = ax.boxplot(data)

# Show plot
plt.xticks([1,2],['user_1','user_2'])
plt.show()

Time Series Plot

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

In [14]:

# Time Series Plot


plt.figure(figsize=(8,6))
ts = pd.Series(np.random.randn(100), index = pd.date_range(
'1/1/2020', periods = 100))
# Return the cumulative sum of the elements.
ts = ts.cumsum()
ts.plot()
plt.show()

In [ ]:

localhost:8888/notebooks/Untitled42.ipynb?kernel_name=python3 8/8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy