Python Practical Guide 2
Python Practical Guide 2
Department of Computing
Table of Contents
Principle of analytic Graphics ....................................................................................................................... 1
Activities ....................................................................................................................................................... 2
Activity 01: Line Chart ............................................................................................................................. 2
Activity 02: Scatter Plot ............................................................................................................................ 3
Activity 3: Using Data in MS Excel ......................................................................................................... 4
Activity 4: Histogram ............................................................................................................................... 6
Activity 5(i): Bubble Chart ....................................................................................................................... 7
Activity 5(ii).............................................................................................................................................. 8
Activity 6: Emulating ggplot..................................................................................................................... 9
Activity 7: Multiple Subplots ................................................................................................................... 10
Activity 8: Exporting Plots ..................................................................................................................... 11
Activity 9(i): Bar Chart ........................................................................................................................... 12
Activity 9 (ii): Differentiate between the networks by applying different colors ................................... 13
Activity 10: Scatter Chart with trend line (Using Plotly Express) .......................................................... 13
Activity 11: Scatter Chart with multiple subplot (Using Plotly Express) ............................................... 14
Activity 12: Animated Chart ................................................................................................................... 15
Activity 13: Map ..................................................................................................................................... 16
Activity 14: Map with geopandas library ............................................................................................... 17
DATA VISUALIZATION WITH MATPLOTLIB
1
DATA VISUALIZATION WITH MATPLOTLIB
Activities
plt.plot(year, pop)
plt.show()
Output:
2
DATA VISUALIZATION WITH MATPLOTLIB
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78, 77, 85, 86]
plt.scatter(x, y, c="blue")
3
DATA VISUALIZATION WITH MATPLOTLIB
4
DATA VISUALIZATION WITH MATPLOTLIB
Output
5
DATA VISUALIZATION WITH MATPLOTLIB
Activity 4: Histogram
Output
6
DATA VISUALIZATION WITH MATPLOTLIB
plt.show()
Output
7
DATA VISUALIZATION WITH MATPLOTLIB
Activity 5(ii)
Output
8
DATA VISUALIZATION WITH MATPLOTLIB
#Emulate ggplot
plt.style.use('ggplot')
#Change the location of the Excel file according to yours
data = pd. ExcelFile('C:\\Users\\HpUser\\Downloads\population_literacy.xlsx')
population_literacy = data.parse('Sheet1')
plt.scatter(population_literacy['Female Literacy'],population_literacy['Fertility'],
s= population_literacy['Fertility'] ** 4,marker='o',c=population_literacy['Fertility'])
#add Title
plt.title('Female Literacy vs. Fertility')
plt.show()
Output
9
DATA VISUALIZATION WITH MATPLOTLIB
10
DATA VISUALIZATION WITH MATPLOTLIB
11
DATA VISUALIZATION WITH MATPLOTLIB
import pandas as pd
import plotly.express as px
#Change the location of the CSV file according to yours
Phone_Data=pd.read_csv('C:\\Users\\Hp User\\Downloads\Phone_Data.csv' )
# Get total duration for each network
total_duration_by_network=Phone_Data.groupby('network')['duration'].sum()
#Convert Series into Dataframe as required by Plotly Express
total_duration_by_network=total_duration_by_network.to_frame('duration').reset_index()
bar_chart =px.bar(total_duration_by_network.reset_index(),x="network",y="duration")
bar_chart.show()
12
DATA VISUALIZATION WITH MATPLOTLIB
import pandas as pd
import plotly.express as px
Phone_Data=pd.read_csv('C:\\Users\\Hp User\\Downloads\Phone_Data.csv' )
#Change the location of the CSV file according to yours
# Get total duration for each network
total_duration_by_network=Phone_Data.groupby('network')['duration'].sum()
#Convert Series into Dataframe as required by Plotly Express
total_duration_by_network=total_duration_by_network.to_frame('duration').reset_index()
bar_chart
=px.bar(total_duration_by_network.reset_index(),x="network",y="duration",color="network")
bar_chart.show()
Activity 10: Scatter Chart with trend line (Using Plotly Express)
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", trendline="ols")
fig.show()
13
DATA VISUALIZATION WITH MATPLOTLIB
Activity 11: Scatter Chart with multiple subplot (Using Plotly Express)
Can easily plot scatter chart using Plotly Express. You can easily create multiple subplots in a
very intuitive manner directly from the scatter function.
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="smoker",
facet_col="sex", facet_row="time")
fig.show()
14
DATA VISUALIZATION WITH MATPLOTLIB
import plotly.express as px
df = px.data.gapminder()
15
DATA VISUALIZATION WITH MATPLOTLIB
Generate a map from a preloaded dataset in Plotly Express. Use the data function
to load gapminder dataset. Gapminder is a dataset about life expectancies across all
the countries in the world.
import plotly.express as px
df = px.data.gapminder().query("year==2007")
fig = px.scatter_geo(df, locations="iso_alpha", color="continent",
hover_name="country", size="pop",
projection="natural earth")
fig.show()
16
DATA VISUALIZATION WITH MATPLOTLIB
Generate a map from a preloaded dataset in Plotly Express. Use the data function
to load geojson dataset.
df = px.data.election()
geo_df = gpd.GeoDataFrame.from_features(
px.data.election_geojson()["features"]
).merge(df, on="district").set_index("district")
fig = px.choropleth(geo_df,
geojson=geo_df.geometry,
locations=geo_df.index,
color="Joly",
projection="mercator")
fig.update_geos(fitbounds="locations", visible=False)
fig.show()
Output:
17