class: center, middle, inverse, title-slide .title[ # ISA 419: Data-Driven Security ] .subtitle[ ## 09: Visualizing Data in Python ] .author[ ###
Fadel M. Megahed, PhD
Professor
Farmer School of Business
Miami University
@FadelMegahed
fmegahed
fmegahed@miamioh.edu
Automated Scheduler for Office Hours
] .date[ ### Spring 2025 ] --- ## Quick Refresher of Last Class ✅ Create quick visualizations using the `plot` method from [pandas](https://pandas.pydata.org/docs/user_guide/visualization.html) (with an understanding of the effect of different backends). ✅ Utilize `auto-viz` type plots to create a quick EDA of your data. --- ## Learning Objectives for Today's Class - Utilize standalone data viz packages to construct and tailor your graphs. - Examine the use of network and/or spatial plots in the context of network data --- class: inverse, center, middle # Utilize Standalone Data Viz Packages to Construct and Tailor your Graphs --- ## Our Data - We will use the `merged_ips` data set from a previous class to demonstrate how to plot data in pandas. .font80[ ``` python import pandas as pd import numpy as np np.random.seed(2025) # so we get the same random sample toxic_ips = pd.read_csv( "https://raw.githubusercontent.com/fmegahed/isa419/main/data/listed_ip_90_all.csv", header = None, names = ['ip', 'frequency', 'lastseen'] ) geolocation = pd.read_csv( 'https://raw.githubusercontent.com/fmegahed/isa419/main/data/ip_geolocation.csv', names = ['ip', 'country', 'city', 'latitude', 'longitude'] ) merged_ips = ( toxic_ips .merge(right = geolocation, how = 'left', on ='ip') .dropna() .assign( lastseen = lambda df: df['lastseen'].astype('datetime64[ns]') ) .sample(1000) #<< a random sample of 1000 rows for faster plotting .query('frequency < 100') ) ``` ] --- ## Grammar of Graphics <img src="data:image/png;base64,#https://www.stat20.org/2-summarizing-data/03-a-grammar-of-graphics/images/grammar-of-graphics.png" alt="The Grammar of Graphics" width="80%" style="display: block; margin: auto;" /> --- ## The Grammar of Graphics with `plotnine` ``` python from plotnine import ggplot, aes, geom_histogram, labs, theme_minimal ( ggplot(merged_ips) # data ) ``` --- ## The Grammar of Graphics with `plotnine` ``` python from plotnine import ggplot, aes, geom_histogram, labs, theme_minimal ( ggplot(merged_ips, aes(x = 'frequency') ) # data + aesthetics ) ``` --- ## The Grammar of Graphics with `plotnine` ``` python from plotnine import ggplot, aes, geom_histogram, labs, theme_minimal ( ggplot(merged_ips, aes(x = 'frequency') ) + # data + aesthetics geom_histogram(bins = 20) # geometry ) ``` --- ## The Grammar of Graphics with `plotnine` ``` python from plotnine import ggplot, aes, geom_histogram, labs, theme_minimal ( ggplot(merged_ips, aes(x = 'frequency') ) + # data + aesthetics geom_histogram(bins = 20) + # geometry labs(title = 'Frequency of Toxic IPs', x = 'Frequency', y = 'Count') + # labels theme_minimal() # theme ) ``` --- ## Class Activity
−
+
05
:
00
.panelset[ .panel[.panel-name[Task] - After going through the [plotnine Reference and Gallery tabs](https://plotnine.org/), create a scatter plot of the `latitude` and `longitude` variables from the `merged_ips` data set using `plotnine`. - Add a title to the plot and label the x and y axes. - Use the `theme_minimal` theme. ] .panel[.panel-name[Solution] ] ] --- class: inverse, center, middle # Examine the Use of Network Plots in the Context of Network Data --- ## Network Data Let us create a simple network data set to demonstrate how to plot network data in Python. We will focus on querying the `merged_ips` data set to only include toxic IPs from Australia. We will create a simple bipartite network with the `ip` and `city` variables. <img src="data:image/png;base64,#../../figures/network_plot.png" width="100%" style="display: block; margin: auto;" /> --- class: inverse, center, middle # Recap --- ## Summary of Main Points By now, you should be able to do the following: - Utilize standalone data viz packages to construct and tailor your graphs. - Examine the use of network and/or spatial plots in the context of network data. --- ## 📝 Review and Clarification 📝 1. **Class Notes**: Take some time to revisit your class notes for key insights and concepts. 2. **Zoom Recording**: The recording of today's class will be made available on Canvas approximately 3-4 hours after the end of class. 3. **Questions**: Please don't hesitate to ask for clarification on any topics discussed in class. It's crucial not to let questions accumulate.