0% found this document useful (0 votes)
62 views48 pages

Pratikum Visualisasi Data-Dikonversi

This document discusses using the Plotnine library in Python to visualize data. It provides examples of creating scatter plots from vehicle fuel economy data to show the relationship between highway and city mileage. Different transmission types are represented with color. It also demonstrates how to customize the plot theme, add bubble plots where the point size represents another variable, and control colors and legends. The examples illustrate the core functionality of Plotnine for simple and customized data visualization.

Uploaded by

Rinda Wahyuni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views48 pages

Pratikum Visualisasi Data-Dikonversi

This document discusses using the Plotnine library in Python to visualize data. It provides examples of creating scatter plots from vehicle fuel economy data to show the relationship between highway and city mileage. Different transmission types are represented with color. It also demonstrates how to customize the plot theme, add bubble plots where the point size represents another variable, and control colors and legends. The examples illustrate the core functionality of Plotnine for simple and customized data visualization.

Uploaded by

Rinda Wahyuni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Visualizing Your Data with Plotnine

https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-
python/

In [1]: satisfied: matplotlib>=3.1.1 in c:\users\tissar\ana conda3\lib\site-


packages (from plotnine) (3.1.1)
pip install plotnine
Requirement already satisfied: statsmodels>=0.9.0 in c:\users\tissar\an
Requiremen aconda3\lib\site-packages (from plotnine) (0.10.1)
t already Requirement already satisfied: patsy>=0.4.1 in c:\users\tissar\anaconda
satisfied: 3\lib\site-packages (from plotnine) (0.5.1)
plotnine Requirement already satisfied: numpy>=1.16.0 in c:\users\tissar\anacond
in a3\lib\site-packages (from plotnine) (1.16.5)
c:\users\t Requirement already satisfied: mizani>=0.6.0 in c:\users\tissar\anacond
issar\anac a3\lib\site-packages (from plotnine) (0.6.0)
onda3\li Requirement already satisfied: pandas>=0.25.0 in c:\users\tissar\anacon
b\site- da3\lib\site-packages (from plotnine) (1.0.3)
packages Requirement already satisfied: scipy>=1.2.0 in c:\users\tissar\anaconda
(0.6.0) 3\lib\site-packages (from plotnine) (1.3.1)
Requiremen Requirement already satisfied: cycler>=0.10 in c:\users\tissar\anaconda
t already 3\lib\site-packages (from matplotlib>=3.1.1->plotnine) (0.10.0)
satisfied: Requirement already satisfied: kiwisolver>=1.0.1 in c:\users\tissar\ana
descartes> conda3\lib\site-packages (from matplotlib>=3.1.1->plotnine) (1.1.0)
=1.1.0 in Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1
c:\users\t in c:\users\tissar\anaconda3\lib\site-packages (from matplotlib>=3.1.1-
issar\anac >plotnine) (2.4.2)
onda3\lib\ Requirement already satisfied: python-dateutil>=2.1 in c:\users\tissar
site- \anaconda3\lib\site-packages (from matplotlib>=3.1.1->plotnine) (2.8.0)
packages
(from
plotnine)
(1.1.0)
Requiremen
t already
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Requirement already satisfied: six in c:\users\tissar\anaconda3\lib\sit
e-packages (from patsy>=0.4.1->plotnine) (1.12.0)
Requirement already satisfied: palettable in c:\users\tissar\anaconda3
\lib\site-packages (from mizani>=0.6.0->plotnine) (3.3.0)
Requirement already satisfied: pytz>=2017.2 in c:\users\tissar\anaconda
3\lib\site-packages (from pandas>=0.25.0->plotnine) (2019.3)
Requirement already satisfied: setuptools in c:\users\tissar\anaconda3
\lib\site-packages (from kiwisolver>=1.0.1->matplotlib>=3.1.1->plotnin
e) (41.4.0)
Note: you may need to restart the kernel to use updated packages.

In [36]: import pandas as


pd import numpy as
np
import matplotlib.pyplot as plt
%matplotlib inline
from plotnine import *
from datetime import datetime, timedelta

Correlation

1. Scatter plot
Scatteplot adalah plot klasik dan fundamental untuk melihat relationship antara variables. Jika
Anda memiliki multiple (lebih dari satu) grup dalam data Anda, dengan plotnine Anda dapat
memvisualisasikan tiap grup dengan warna yang berbeda.

Menyiapkan Data

In [2]: # Import Data


mpg = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/m
aster/mpg_ggplot2.csv")
mpg
Out[2]: manufacturer model displ year cyl trans drv cty hwy fl class

0 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact

1 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact

2 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact

3 audi a4 2.0 2008 4 auto(av) f 21 30 p compact

4 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact

... ... ... ... ... ... ... ... ... ... ... ...

229 volkswagen passat 2.0 2008 4 auto(s6) f 19 28 p midsize

230 volkswagen passat 2.0 2008 4 manual(m6) f 21 29 p midsize

231 volkswagen passat 2.8 1999 6 auto(l5) f 16 26 p midsize

232 volkswagen passat 2.8 1999 6 manual(m5) f 18 26 p midsize

233 volkswagen passat 3.6 2008 6 auto(s6) f 17 26 p midsize

234 rows × 11 columns

In [3]: g = (
ggplot(mpg, aes(x = 'hwy', y ='cty', color = 'trans')) #koordinat x = v
ariabel hwy, koordinat y= cty, warna berdasar var trans
+ labs(x="Highway Miles per Gallon", y="City Mileage in Miles per
Galo n") # memberi nama koordinat x dan y
+ labs(color = 'transmision')# memberi nama legend
+ ggtitle("Fuel Consumption")# memberi judul
)
g
Out[3]: <ggplot: (-9223371880167378888)>

In [4]: g + geom_point() #jenis plot


Out[4]: <ggplot: (-9223371880167378868)>

THEME UNTUK DEKORASI GRAFIK: MENGATUR POSISI LEGEND,


UKURAN GRAFIK,DLL

Anda dapat memberikan pengaturan tambahan untuk plot Anda dengan >> theme

g + geom_point() + theme ( legend_position=(.7,


In [6]: .175),#mengatur posisi legend, silahkan coba dirubah dan lihat pergeserannya
legend_direction='horizontal',#mengatur arah legend. Pilihan vertic al dan horizontal. Default: vertical. Jadi klo anda ingi legend anda ve rtikal Anda tidak perlu menuliskan code legend_d
legend_background = element_rect(color='gray', size=10, fill='gray'
axis_line=element_line(size=2),#mengatur ketebalan garis koordinat
axis_line_x=element_line(color='red'),#mengatur warna garis koordin at x
axis_line_y=element_line(color='blue'),#mengatur warna garis koordi nat y

axis_text=element_text(margin={'t': 5, 'r': 5}), #mengatur margin n ama koordinat


axis_text_x=element_text(color='green'),#mengatur warna nama koordi nat x
axis_text_y=element_text(color='purple') #mengatur warna nama koord inat y
)
Out[6]: <ggplot: (-9223371880123268492)>

Tidak semua code dalam theme perlu dituliskan. Tulis sesuai kebutuhan Anda saja, CONTOH:

In [7]: g + geom_point() +
theme( panel_background=element_rect(fill='gray',
alpha=.2), figure_size=(12, 12),
aspect_ratio=1/3)

Out[7]: <ggplot: (-9223371880123336392)>

DEFAULT THEME DI PLOTNINE

In [8]: g + geom_point() + theme_xkcd()


Out[8]: <ggplot: (-9223371880123309552)>

In [9]: g + geom_point()+ theme_bw()


Out[9]: <ggplot: (-9223371880123153832)>

In [10]: g + geom_point() + theme_classic()


Out[10]: <ggplot: (-9223371880122909512)>

https://plotnine.readthedocs.io/en/stable/generated/plotnine.themes.theme.html

In [11]: g + geom_point()+ theme_dark()


Out[11]: <ggplot: (-9223371880123153888)>

In [12]: g + geom_point()+ theme_light()


Out[12]: <ggplot: (-9223371880123163736)>

In [13]: g + geom_point()+ theme_minimal()


Out[13]: <ggplot: (-9223371880123324780)>

In [14]: g + geom_point()+ theme_void()


Out[14]: <ggplot: (-9223371880123474440)>

BUBBLE PLOT
sama seperti scatter plot sebelumnya, hanya kita tambahkan ukuran dot nya berdasar variabel
numerik tertentu. Misal pada scatter plot di atas ukuran dot dibuat berdasarka variabel cty, maka
tuliskan code berikut:

In [15]: (
ggplot(mpg, aes(x = 'hwy', y ='cty', color = 'trans', size = 'cty')) #t ambahkan size disini
+ labs(x="Highway Miles per Gallon", y="City Mileage in Miles per Galo n")
+ labs(color = 'Cylinder')
+ ggtitle("Fuel Consumption")
+ guides(size = False) #karena variabel penentu size sama dengan koordi nat y, kita tidak perlu tampilkan sebagai legend
+ geom_point()
)

Out[15]: <ggplot: (-9223371880123094124)>

In [16]: (
ggplot(mpg, aes(x = 'hwy', y ='cty', color = 'cyl', size = 'cty'))
+ labs(x="Highway Miles per Gallon", y="City Mileage in Miles per Galo n")
+ labs(color = 'Cylinder')
+ ggtitle("Fuel Consumption")
+ guides(size = False)
+ geom_point()
+ scale_color_gradient(low='green', high='lightgreen') #seting warna
)

Out[16]: <ggplot: (-9223371880123100776)>

In [17]: (
ggplot(mpg, aes(x = 'hwy', y ='cty', color = 'cyl', size ='cyl',shape = 'class'))
+ labs(x="Highway Miles per Gallon", y="City Mileage in Miles per Galo n")
+ labs(size = 'Cylinder')
+ labs(shape = 'Class')
+ ggtitle("Fuel Consumption")
+ guides(size = False)
+ theme ( legend_position=(.7, .2), legend_direction='horizontal',
legend_background = element_rect(color='gray', size=2, fill='gray', alpha=.0),
panel_background = element_rect(fill='gray', alpha=.2), figure_size=(12, 12),
aspect_ratio=1/1
)
+ geom_point(alpha=0.5) #mengatur transparansi dot agar data yang tumpa ng tidndih lebih keliatan
)
Out[17]: <ggplot: (-9223371880123197472)>

MEMBUAT DIAGRAM BATANG


In [18]: ggplot(mpg, aes(x="manufacturer", fill = "manufacturer")) + geom_bar()

Out[18]: <ggplot: (-9223371880123340384)>

nama koordinat x pada diagram batang di atas saling tumpang tindih, kita dapat mengatasinya
dengan menukar posisi manufactur dan count dengan coord_flip()
In [19]: (
ggplot(mpg,aes(x='manufacturer', fill='manufacturer'))
+ geom_bar(show_legend=False)
+ coord_flip()

Out[19]: <ggplot: (-9223371880123191660)>

In [20]: (
ggplot(mpg,aes(x='manufacturer', fill='manufacturer'))
+ geom_bar(show_legend=False)
+ coord_flip()
+ theme_classic()
)

Out[20]: <ggplot: (-9223371880122502352)>

In [21]: (
ggplot(mpg,aes(x='manufacturer', fill='manufacturer'))
+ geom_bar(show_legend=False)
+ coord_flip()
+ theme_minimal()
)
Out[21]: <ggplot: (-9223371880123198552)>

In [22]: (
ggplot(mpg,aes(x='manufacturer', fill='manufacturer'))
+ geom_bar(show_legend=False)
+ coord_flip()
+ theme_xkcd()
)
Out[22]: <ggplot: (-9223371880122933564)>

WRAP
In [23]: (
ggplot(mpg,aes(x='manufacturer', fill='manufacturer'))
+ facet_wrap("year")
+ geom_bar(show_legend=False)
+ coord_flip()

)
Out[23]: <ggplot: (-9223371880122518516)>

In [24]: (
ggplot(mpg,aes(x='manufacturer', fill='class'))
+ facet_grid("year~cyl")
+ geom_bar()
+ coord_flip()

)
Out[24]: <ggplot: (-9223371880122904608)>

Mengurutkan Data Berdasarkan Count


Manufacturer
In [25]: mpg2 = mpg.copy() mpg2

Out[25]:
manufacturer model displ year cyl trans drv cty hwy fl class

0 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact

1 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact

2 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact


manufacturer model displ year cyl trans drv cty hwy fl class

3 audi a4 2.0 2008 4 auto(av) f 21 30 p compact

4 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact

... ... ... ... ... ... ... ... ... ... ... ...

229 volkswagen passat 2.0 2008 4 auto(s6) f 19 28 p midsize

230 volkswagen passat 2.0 2008 4 manual(m6) f 21 29 p midsize

231 volkswagen passat 2.8 1999 6 auto(l5) f 16 26 p midsize

232 volkswagen passat 2.8 1999 6 manual(m5) f 18 26 p midsize

233 volkswagen passat 3.6 2008 6 auto(s6) f 17 26 p midsize

234 rows × 11 columns

In [26]: manufacturer_order = mpg2['manufacturer'].value_counts().index


manufacturer_order
Out[26]: Index(['dodge', 'toyota', 'volkswagen', 'ford', 'chevrolet', 'audi', 'h
yundai',
'subaru', 'nissan', 'honda', 'jeep', 'pontiac', 'mercury', 'land
rover',
'lincoln'],
dtype='object')

In [27]: mpg2['manufacturer'] = pd.Categorical(mpg2['manufacturer'], categories=


manufacturer_order, ordered=True)

In [28]: mpg2['manufacturer']

Out[28]: 0 audi
1 audi
2 audi
3 audi
4 audi
...
229 volkswagen
230 volkswagen
231 volkswagen
232 volkswagen
233 volkswagen
Name: manufacturer, Length: 234, dtype: category
Categories (15, object): [dodge < toyota < volkswagen < ford ... pontia
c < mercury < land rover < lincoln]

In [29]: (
ggplot(mpg2,aes(x='manufacturer', fill='manufacturer'))
+ geom_bar(show_legend=False)
+ coord_flip()
+ theme_xkcd()
+ ggtitle('Number of Cars by Manufacturer')
)
Out[29]: <ggplot: (-9223371880122834348)>

GRAFIK TIME SERIES


In [44]: # Import Data
corona = pd.read_csv("https://covid.ourworldindata.org/data/ecdc/full_d
ata.csv")
corona

Out[44]:
date location new cases new deaths total cases total deaths
date location new_cases new_deaths total_cases total_deaths

0 2019-12-31 Afghanistan 0 0 0 0
1 2020-01-01 Afghanistan 0 0 0 0

2 2020-01-02 Afghanistan 0 0 0 0

3 2020-01-03 Afghanistan 0 0 0 0

4 2020-01-04 Afghanistan 0 0 0 0

... ... ... ... ... ... ...

8393 2020-03-29 Zimbabwe 2 0 7 1

8394 2020-03-30 Zimbabwe 0 0 7 1

8395 2020-03-31 Zimbabwe 0 0 7 1

8396 2020-04-01 Zimbabwe 1 0 8 1

8397 2020-04-02 Zimbabwe 0 0 8 1

8398 rows × 6 columns

In [45]: Indonesia = corona[corona['location'] ==


'Indonesia'] Indonesia= Indonesia.reset_index(drop =
True) Indonesia
Out[45]:
date location new_cases new_deaths total_cases total_deaths

0 2019-12-31 Indonesia 0 0 0 0

1 2020-01-01 Indonesia 0 0 0 0

2 2020-01-02 Indonesia 0 0 0 0

3 2020-01-03 Indonesia 0 0 0 0

4 2020-01-04 Indonesia 0 0 0 0

... ... ... ... ... ... ...

82 2020-03-29 Indonesia 109 15 1155 102


date location new_cases new_deaths total_cases total_deaths

83 2020-03-30 Indonesia 130 12 1285 114

84 2020-03-31 Indonesia 129 8 1414 122

85 2020-04-01 Indonesia 114 14 1528 136

86 2020-04-02 Indonesia 149 21 1677 157

87 rows × 6 columns

In [46]: ggplot(Indonesia, aes(x='date', y='new_cases')) + geom_line()

C:\Users\Tissar\Anaconda3\lib\site-packages\plotnine\geoms\geom_path.p
y:83: PlotnineWarning: geom_path: Each group consist of only one observ
ation. Do you need to adjust the group aesthetic?
"group aesthetic?", PlotnineWarning)
Out[46]: <ggplot: (-9223371942254681300)>

COSTUMIZE DATE
In [48]: IndonesiaTS=Indonesia.copy() #copy dataframe Indonesia
IndonesiaTS.date = pd.to_datetime(IndonesiaTS.date)
IndonesiaTS.set_index('date', inplace=True) #mengubah date menjadi
inde x
IndonesiaTS

Out[48]:
location new_cases new_deaths total_cases total_deaths
date location new_cases new_deaths total_cases total_deaths

date

2019-12-31 Indonesia 0 0 0 0
2020-01-01 Indonesia 0 0 0 0

2020-01-02 Indonesia 0 0 0 0

2020-01-03 Indonesia 0 0 0 0

2020-01-04 Indonesia 0 0 0 0

... ... ... ... ... ...

2020-03-29 Indonesia 109 15 1155 102

2020-03-30 Indonesia 130 12 1285 114

2020-03-31 Indonesia 129 8 1414 122

2020-04-01 Indonesia 114 14 1528 136

2020-04-02 Indonesia 149 21 1677 157

87 rows × 5 columns

Mengambil Tanggal Awal Ditemukan Kasus Sampai


Dengan Sekarang
In [49]: IndonesiaTSDate = IndonesiaTS['2020-03-01':
datetime.today().strftime( '%Y-%m-%d')]
IndonesiaTSDate

Out[49]:
location new_cases new_deaths total_cases total_deaths

date

2020-03-01 Indonesia 0 0 0 0
2020-03-02 Indonesia 2 0 2 0

Create PDF in your applications with the Pdfcrowd


location new_cases new_deaths total_cases total_deaths

date

2020-03-07 Indonesia 2 0 4 0

2020-03-09 Indonesia 2 0 6 0

2020-03-11 Indonesia 13 0 19 0

2020-03-12 Indonesia 15 1 34 1

2020-03-14 Indonesia 35 3 69 4

2020-03-15 Indonesia 27 0 96 4

2020-03-16 Indonesia 21 1 117 5

2020-03-17 Indonesia 17 0 134 5

2020-03-18 Indonesia 38 0 172 5

2020-03-19 Indonesia 0 0 172 5

2020-03-20 Indonesia 55 14 227 19

2020-03-21 Indonesia 82 6 309 25

2020-03-22 Indonesia 141 13 450 38

2020-03-23 Indonesia 64 10 514 48

2020-03-24 Indonesia 65 1 579 49

2020-03-25 Indonesia 107 6 686 55

2020-03-26 Indonesia 104 3 790 58

2020-03-27 Indonesia 103 20 893 78

2020-03-28 Indonesia 153 9 1046 87

2020-03-29 Indonesia 109 15 1155 102

2020-03-30 Indonesia 130 12 1285 114

2020-03-31 Indonesia 129 8 1414 122

2020-04-01 Indonesia 114 14 1528 136

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
2020-04-02 Indonesia 149 21 1677 157

In [50]: IndonesiaTSDate_resetindex = IndonesiaTSDate.reset_index() #mengembalik


an date menjadi header kolom
IndonesiaTSDate_resetindex
Out[50]:
date location new_cases new_deaths total_cases total_deaths

0 2020-03-01 Indonesia 0 0 0 0

1 2020-03-02 Indonesia 2 0 2 0

2 2020-03-07 Indonesia 2 0 4 0

3 2020-03-09 Indonesia 2 0 6 0

4 2020-03-11 Indonesia 13 0 19 0

5 2020-03-12 Indonesia 15 1 34 1

6 2020-03-14 Indonesia 35 3 69 4

7 2020-03-15 Indonesia 27 0 96 4

8 2020-03-16 Indonesia 21 1 117 5

9 2020-03-17 Indonesia 17 0 134 5

10 2020-03-18 Indonesia 38 0 172 5

11 2020-03-19 Indonesia 0 0 172 5

12 2020-03-20 Indonesia 55 14 227 19

13 2020-03-21 Indonesia 82 6 309 25

14 2020-03-22 Indonesia 141 13 450 38

15 2020-03-23 Indonesia 64 10 514 48

16 2020-03-24 Indonesia 65 1 579 49

17 2020-03-25 Indonesia 107 6 686 55

18 2020-03-26 Indonesia 104 3 790 58

19 2020-03-27 Indonesia 103 20 893 78


date location new_cases new_deaths total_cases total_deaths

20 2020-03-28 Indonesia 153 9 1046 87

21 2020-03-29 Indonesia 109 15 1155 102

22 2020-03-30 Indonesia 130 12 1285 114

23 2020-03-31 Indonesia 129 8 1414 122

24 2020-04-01 Indonesia 114 14 1528 136

25 2020-04-02 Indonesia 149 21 1677 157

In [51]: (
ggplot(IndonesiaTSDate_resetindex, aes(x='date', y='new_cases'))
+ geom_line(color='red')
+ ggtitle('Corona New Cases in Indonesia')
+ theme ( legend_position=(.7, .175),#mengatur posisi legend, silahkan
coba dirubah dan lihat pergeserannya
legend_direction='horizontal',#mengatur arah legend. Pilihan
vertic al dan horizontal. Default: vertical. Jadi klo anda ingi legend
anda ve rtikal Anda tidak perlu menuliskan code legend_direction
legend_background = element_rect(color='gray', size=10, fill='gray'
, alpha=.1),#mengatur warna border legend, ketebalan border legend, wa
rna background legend,transparancy background legend
panel_background = element_rect(fill='gray', alpha=.2),#warna
backg round plot,transparancy background plot
figure_size=(12, 12), # mengatur ukuran plot dalam inches
aspect_ratio=1/1
)
)
Out[51]: <ggplot: (-9223371942257239412)>
MEMBUAT PLOT TIME SERIES DENGAN DUA
SKALA DATA

In [17]: x = Indonesia['date']
y1 = Indonesia['new_cases']
y2 = Indonesia['total_cases']

# Plot Line1 (Left Y Axis)


fig, ax1 = plt.subplots(1,1,figsize=(16,9), dpi= 80)
ax1.plot(x, y1, color='tab:red')

# Plot Line2 (Right Y Axis)


ax2 = ax1.twinx() # instantiate a second axes that shares the same x-a
xis
ax2.plot(x, y2, color='tab:blue')

# Decorations
# ax1 (left Y axis)
ax1.set_xlabel('Date', fontsize=20)
ax1.tick_params(axis='x', rotation=0, labelsize=12)
ax1.set_ylabel('New Cases', color='tab:red', fontsize=20)
ax1.tick_params(axis='y', rotation=0, labelcolor='tab:red' )
ax1.grid(alpha=.4)

# ax2 (right Y axis)


ax2.set_ylabel("Total Cases", color='tab:blue', fontsize=20)
ax2.tick_params(axis='y', labelcolor='tab:blue')
ax2.set_xticks(np.arange(10, len(x), 10))
ax2.set_xticklabels(x[::60], rotation=90, fontdict={'fontsize':10})
ax2.set_title("New Casses vs Total Cases in Indonesia: Plotting in Sec
ondary Y Axis", fontsize=22)

fig.tight_layout()
plt.show()
PLOT TS DUA SKALA UNTUK TANGGAL 2
MARET 2020 HINGGA HARI INI
In [15]: IndonesiaTSDate_resetindex = IndonesiaTSDate.reset_index()
IndonesiaTSDate_resetindex
Out[15]:
date location new_cases new_deaths total_cases total_deaths

0 2020-03-01 Indonesia 0 0 0 0

1 2020-03-02 Indonesia 2 0 2 0

2 2020-03-07 Indonesia 2 0 4 0

3 2020-03-09 Indonesia 2 0 6 0
date location new_cases new_deaths total_cases total_deaths

4 2020-03-11 Indonesia 13 0 19 0

5 2020-03-12 Indonesia 15 1 34 1

6 2020-03-14 Indonesia 35 3 69 4

7 2020-03-15 Indonesia 27 0 96 4

8 2020-03-16 Indonesia 21 1 117 5

9 2020-03-17 Indonesia 17 0 134 5

10 2020-03-18 Indonesia 38 0 172 5

11 2020-03-19 Indonesia 0 0 172 5

12 2020-03-20 Indonesia 55 14 227 19

13 2020-03-21 Indonesia 82 6 309 25

14 2020-03-22 Indonesia 141 13 450 38

15 2020-03-23 Indonesia 64 10 514 48

16 2020-03-24 Indonesia 65 1 579 49

17 2020-03-25 Indonesia 107 6 686 55

18 2020-03-26 Indonesia 104 3 790 58

19 2020-03-27 Indonesia 103 20 893 78

20 2020-03-28 Indonesia 153 9 1046 87

21 2020-03-29 Indonesia 109 15 1155 102

22 2020-03-30 Indonesia 130 12 1285 114

23 2020-03-31 Indonesia 129 8 1414 122

24 2020-04-01 Indonesia 114 14 1528 136

25 2020-04-02 Indonesia 149 21 1677 157

In [54]: x = IndonesiaTSDate_resetindex['date']
y1 = IndonesiaTSDate_resetindex['new_cases'] y2 = IndonesiaTSDate_resetindex['total_cases']
# Plot Line1 (Left Y Axis)
fig, ax1 = plt.subplots(1,1,figsize=(12,6), dpi= 180)
ax1.plot(x, y1, color='tab:red')

# Plot Line2 (Right Y Axis)


ax2 = ax1.twinx() # instantiate a second axes that shares the same x-a
xis
ax2.plot(x, y2, color='tab:blue')

# Decorations
# ax1 (left Y axis)
ax1.set_xlabel('Date', fontsize=20)
ax1.tick_params(axis='x', rotation=0, labelsize=12)
ax1.set_ylabel('New Cases', color='tab:red', fontsize=20)
ax1.tick_params(axis='y', rotation=0, labelcolor='tab:red' )
ax1.grid(alpha=.2)

# ax2 (right Y axis)


ax2.set_ylabel("Total Cases", color='tab:blue', fontsize=20)
ax2.tick_params(axis='y', labelcolor='tab:blue')
ax2.set_xticks(np.arange(0, len(x), 30))
ax2.set_xticklabels(x[::60], rotation=90, fontdict={'fontsize':10})
ax2.set_title("New Casses vs Total Cases in Indonesia: 1 March - 2 Apr
il 2020", fontsize=22)

fig.tight_layout()
plt.show()
sumber data: h
ttps://covid.ourworldindata.org/data/ecdc/full_data.csv

In [ ]:

In [ ]:

You might also like