FB Style Menu

Tuesday, February 15, 2022

Data Virtualization by Python - Line Chart

# Importing pandas and matplotlib
import pandas as pd
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot

# Import excel or csv
# You can copy and paste sample data in down there - > Table data
Mydata = pd.read_excel('D:\\file path\\file name.xlsx')
Mydata.head(2)











# Grouping data
Sumdata = Mydata.groupby('Region').sum()
Sumdata















# Line Chart one line
# Figure size setting
plt.figure(figsize=(6,5))

plt.plot(Mydata2.index, Mydata2['Data Traffic (MB)'],color ="#003A99")

plt.title("Attach User and Traffic",fontsize=16, fontweight="bold")
plt.xlabel('Attach User',fontsize=13)
plt.ylabel("Total traffic (MB)",fontsize = 13)

#Rotation label words
plt.xticks(rotation=45)

plt.show()





















-----------------------------------------------------------------------------------------------
# Line Chart two lines
from  matplotlib import pyplot as plt
plt.plot(Mydata2.index, Mydata2['Attach User'],label = "User",color ="Red")
plt.plot(Mydata2.index, Mydata2['Data Traffic (MB)'],label = "Traffic",color ="Green")

plt.title("Attach User and Traffic",fontsize=14, fontweight="bold")
plt.xlabel('Region',fontsize=13)
plt.ylabel("Total traffic (MB)",fontsize = 13)

# show a legend on the plot
plt.legend()

plt.show()


















----------------------------------------------------------------------------------------------

# Labeling on both Y axes
Graph,A = plt.subplots()
Graph.set_size_inches(8, 4, forward=True)

A.plot(Mydata2.index,Mydata2['Attach User'],marker="+",label='User',color="Green")
A.set_xlabel("Region")
A.set_ylabel("Attach User")

B=A.twinx()
B.plot(Mydata2.index,Mydata2['Data Traffic (MB)'],marker="o",label='Traffic',color="blue")
B.set_ylabel("Traffic(MB)")

plt.title('Traffic and User per site')
Graph.legend()

#Rotation x label words
#below two lines of code got warning
#xlabels = Mydata['Site Name']
#A.set_xticklabels(xlabels, rotation=45)
#A.set_xticks(rotation=45)

plt.show()













--------------------------------------------------------------------------------

Table data :

Region

Site Name

Date

Attach User

Data Traffic (MB)

Technology

Yangon

YGN001

23-09-2021

79

2995

4G

Yangon

YGN002

23-09-2021

126

5450

4G

Yangon

YGN003

23-09-2021

260

16710

4G

Mandalay

MDY001

23-09-2021

163

8768

4G

Mandalay

MDY002

23-09-2021

7

0

4G

Mandalay

MDY003

23-09-2021

223

13154

4G

NayPyiTaw

NPT001

23-09-2021

236

9250

3G

NayPyiTaw

NPT002

23-09-2021

172

5739

4G

NayPyiTaw

NPT003

23-09-2021

318

7265

4G

Shan

SHAN001

23-09-2021

195

6590

3G

Shan

SHAN002

23-09-2021

205

2948

4G

Mon

MON001

23-09-2021

31

2156

3G

Mon

MON002

23-09-2021

138

4743

4G

Ayarwaddy

AYA001

23-09-2021

63

1576

3G

Ayarwaddy

AYA002

23-09-2021

145

3245

4G


Reference :
https://cmdlinetips.com/2019/10/how-to-make-a-plot-with-two-different-y-axis-in-python-with-matplotlib/#:~:text=The%20way%20to%20make%20a,by%20updating%20the%20axis%20object.

2 comments: