Header Ads

Use Python to visualize data: Lesson#1

 

www.datatalk.site

Lesson#1: Use python to plot data

Today I will show you the way to present data beautifully in a python environment. For the current session, we will use Jupyter Notebook under Anaconda Navigator to implement the Python language. I will show from scratch for your better understanding.

Step#1: Download Anaconda from https://www.anaconda.com/products/individual and install individual version for the free version.

Step#2: After installation, Open Anaconda

Step#3: Go to the home tab, click on “Install” to install the Jupyter Notebook within anaconda, and lunch the program.

Step#4: It will open through your default browser.

Step#5: Now, you have to create a folder to save your work in the folder for easy going. Find “New” tab at the right corner under the Files section and click on it and create the folder.

Step#6: Select “Untitled Folder” and Rename it as you wish.

Step#7: Enter into the folder and create a script file using python, and click on ‘untitled’ and you can rename your script file.

Step#8: In [ ]: is the code writing blank line. Now start your code writing from here

Install “matplotlib”, go to Anaconda again and Click on Environment Section, where you find “Search Package” window after selecting the dropdown menu from immediate left of Channel Tab. Write ‘matplotlib’ and hit enter. You get the required package and install it in the anaconda to implement it in your Jupyter Notebook.


Code

#Import the required library

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

##Import your data: use .csv file to import

# I will use data retrieved from ourworldindata.org

data=pd.read_csv('B:\WebBuild\Post_Document\Lesson_part\international-tourist-arrivals-by-world-region.csv', header=0)

# As we use time series to plot the data, we will define index_col function to set the Year/Month Variable

data=pd.read_csv('B:\WebBuild\Post_Document\Lesson_part\international-tourist-arrivals-by-world-region.csv', header=0, index_col=0)

 

# To view the data call the name for your defined dataframe; we what to view first five row

data.head(5)

# Now plot the data: one by one variable

plt.plot(data.Africa, color = 'black', label = 'Africa')# to select 'Africa' column or variable

plt.plot(data.America, color = 'red', label = 'America')# to select 'America' variable

plt.plot(data.AsiaPaci, color = 'green', label = 'Asia & Pacific')# to select 'Asia & Pacific' variable

plt.plot(data.Europe, color = 'blue', label = 'Europe')# to select 'Europe' variable

plt.plot(data.MidEast, color = 'yellow', label = 'Middle East')# to select 'Middle East' variable

# Individual graphs were shown but we need in a single graph< Just wait and see

##Give title and label of axis

plt.plot(data.Africa, color = 'black', label = 'Africa')

plt.plot(data.America, color = 'red', label = 'America')

plt.plot(data.AsiaPaci, color = 'green', label = 'Asia & Pacific')

plt.plot(data.Europe, color = 'blue', label = 'Europe')

plt.plot(data.MidEast, color = 'yellow', label = 'Middle East')

plt.title('International Tourist Arrivals by World Region')

plt.xlabel('Time')

plt.ylabel('Number of Arrivals (100 Million)')

plt.legend()

plt.show()


Final Output




Note:

Grateful to Anaconda and Python for using their platform to prepare this lesson. Data used in the tutorial retrieved from ourworldindata.org

                                                    


Prepared by

Shuvro Guda

Data Analyst

Unnayan Shamannay

No comments

Powered by Blogger.