Week3:
PART-A: usage of plotly and pandas
Part-B: usage of matplotlib and pandas
Outcomes of this week are:
Aim: To write a Python program for creating Maps using Plotly and Pandas Libraries
offline maps : Example-A-1, A-2, A-3
online maps: Example-A-4, A-5, A-6
Aim: To write a Python program that illustrates Linear Plotting using Matplotlib
plotly is a library with a set of functions like maps, 2D, 3D visualizations. But the experiments are only mentioned for MAPS....
to install plotly, use the below code in command prompt:
python -m pip install plotly
scatter ---> on a map, the required info is at distinct/ individual points.
Example: Bengaluru, NewDelhi, Hyderabad, Secunderabad,....
A choropleth map uses colors or shades to represent statistical data across predefined geographic areas (like countries, states, or counties) to show patterns, making it easy to see regional differences in variables like population density, income, or election results. Darker shades typically indicate higher values, while lighter shades show lower values, allowing for quick visual comparison of data across regions.
see the below examples/ figures:
#example-A-1: just view the map that is inbuilt of plotly libraries
import plotly.express as px
fig = px.scatter_geo()
fig.show()
#example-A-2: highlight Bengaluru in map (offline ---> using plotly) using Pandas (create a DataFrame with latitude and longitude of Bengaluru)
import pandas as pd
import plotly.express as px
df = pd.DataFrame({"lat": [13.0827, 12.9716], "lon": [80.2707, 77.5946], "city": ["Chennai", "Bengaluru"]})
fig = px.scatter_geo(df, lat="lat", lon="lon", text="city")
fig.show()
#Exercise-A-3: highlight (NewDelhi, Kakinada) in map (offline ---> using plotly) using Pandas (create a DataFrame with latitude and longitude of both cities)
import pandas as pd
import plotly.express as px
data= {?????}
df = pd.DataFrame({?????})
fig = px.scatter_geo(?????)
fig.show()
#example-A-4:
import plotly.express as px
fig = px.scatter_map(lat=[0.0],lon=[0.0],zoom=5)
fig.update_layout(mapbox_style="open-street-map")
fig.show()
# observe the above stament is scatter_mapbox() ---> scatter_map()
# mapbox is an older version;
#example-A-5: highlight Bengaluru in map (Online---> using plotly) using Pandas (create a DataFrame with latitude and longitude of Bengaluru)
import pandas as pd
import plotly.express as px
df = pd.DataFrame({"lat": [13.0827, 12.9716], "lon": [80.2707, 77.5946], "city": ["Chennai", "Bengaluru"]})
fig = px.scatter_map(df, lat="lat", lon="lon", text="city")
fig.show()
by hovering mouse at Bengaluru, it shows city, lat, long
#Exercise-A-6: highlight (NewDelhi, Kakinada) in map (Online---> using plotly) using Pandas (create a DataFrame with latitude and longitude of both cities)
import pandas as pd
import plotly.express as px
data= {?????}
df = pd.DataFrame({?????})
fig = px.scatter_map(?????)
fig.show()
Example-A-7: using choropleth based on country location
import pandas as pd
import plotly.express as px
df = pd.DataFrame({"country": ["IND", "USA", "CHN"],"value": [1400, 331, 1440]})
fig = px.choropleth(df,locations="country",locationmode="ISO-3",color="value",title="Simple Choropleth Map")
fig.show()
observe the color variations: IND= 1400, CHN = 1440... they can not be differentiated using colors too much as they vary by a vlaue of 40.... to indicate they are neighbouring countries
Example-A-8: using choropleth to highlight country on the basis of GDP
import pandas as pd
import plotly.express as px
df=pd.DataFrame({"iso": ["IND", "USA", "CHN"],"gdp": [3.4, 26.9, 17.7]})
fig = px.choropleth(df,locations="iso",locationmode="ISO-3",color="gdp",title="GDP-based Choropleth Map")
fig.show()
Example-A-9: using choropleth to highlight country on the basis of Continent (= here in code is called scope)
import pandas as pd
import plotly.express as px
df=pd.DataFrame({"iso": ["IND", "USA", "CHN"],"value":[1400,365,1440]})
fig = px.choropleth(df,locations="iso",locationmode="ISO-3",color="value",scope="asia",title="Asia-only Choropleth")
fig.show()
#various scopes are: "world", "asia", "europe","africa", "north america", "south america"
Example-A-10: using choropleth_mapbox()
import pandas as pd
import plotly.express as px
df = pd.DataFrame({"country": ["IND", "USA", "CHN"],"value": [1400, 331, 1440]})
fig = px.choropleth_mapbox(df,locations="country",color="value",mapbox_style="open-street-map",zoom=1,title="Simple Choropleth Map")
fig.show()
Example-A-11: using choropleth_mapbox() to highlight country on the basis of GDP
import pandas as pd
import plotly.express as px
import json
import urllib.request
# Load world country boundaries (GeoJSON with ISO-3 codes)
#json files required for choropleth_mapbox() to define boundaries
url = "https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json"
with urllib.request.urlopen(url) as response:
world_geojson = json.load(response)
df = pd.DataFrame({"iso": ["IND", "USA", "CHN"],"gdp": [3.4, 26.9, 17.7]})
fig = px.choropleth_mapbox(df,geojson=world_geojson,locations="iso",featureidkey="id",color="gdp",mapbox_style="open-street-map",zoom=1,center={"lat": 20, "lon": 0},title="GDP-based Choropleth Map (IND, USA, CHN)")
fig.show()
matplotlib is a library that has various functions like
to install, use below code in command prompt:
python -m pip install matplotlib
Plotting Functions
These functions generate the visual representation of data:
plot(): Plots y versus x as lines and/or markers, used for line graphs and scatter plots.
scatter(): Creates a scatter plot of y versus x.
bar(): Makes a vertical bar plot.
hist(): Plots a histogram to show the distribution of data.
pie(): Creates a pie chart.
imshow(): Displays data as an image (e.g., a heatmap) on a 2D regular raster.
contour() and contourf(): Draw contour lines and filled contour regions, respectively.
stem(): Creates a stem plot (vertical lines with markers at their tips).
Figure and Axes Management
These functions control the overall layout and structure of the visualization:
figure(): Creates a new figure, which is the top-level container for all plot elements.
subplots(): A convenience function that creates a figure and a set of subplots (Axes) in a single call.
subplot(): Adds an Axes (plot area) to the current figure at a specific grid position.
gcf(): Gets the current figure.
gca(): Gets the current Axes.
clf(): Clears the current figure.
cla(): Clears the current Axes.
Axis Configuration and Customization
These functions are used to refine the appearance and labels of a plot:
xlabel() and ylabel(): Set the labels for the x and y axes.
title(): Sets a title for the current Axes.
suptitle(): Adds a centered super title to the entire figure.
xlim() and ylim(): Get or set the limits (minimum and maximum values) of the x and y axes.
grid(): Configures the grid lines on the plot.
legend(): Places a legend on the Axes.
colorbar(): Adds a color bar to a plot.
Output Functions
show(): Displays all open figures and starts the GUI event loop.
savefig(): Saves the current figure to a file (e.g., PNG, PDF, SVG).
matplotlib usage: import matplotlib.pyplot as plt
Example-B-1:
import pandas as pd
import matplotlib.pyplot as plt
data = {"x": [1, 2, 3, 4, 5],"y": [2, 4, 6, 8, 10]}
df = pd.DataFrame(data)
plt.plot(df["x"], df["y"])
plt.xlabel("X values")
plt.ylabel("Y values")
plt.title("Linear Plot using Matplotlib")
plt.show()
choropleth_mapbox() fields must match with the GeoJSON fields
What is Plotly in Python?
Plotly is a Python visualization library used for creating interactive charts and maps, including offline and online geographic plots.
How do you install Plotly in your Python environment?
You install Plotly using pip with the command: python -m pip install plotly.
What is the difference between offline and online maps in Plotly?
Offline maps use functions like scatter_geo(), which display maps without external map tiles; online maps use functions like scatter_map() with mapbox styles for web-based tile rendering.
How is a Pandas DataFrame used with Plotly maps?
A DataFrame holds latitude and longitude values along with labels; Plotly functions like px.scatter_geo() accept DataFrame columns to plot points on maps.
What does scatter_geo() do?
scatter_geo() creates an offline geographic scatter map chart that marks points at given latitude and longitude positions.
How do you create an online map using Plotly?
Use px.scatter_map() and call fig.update_layout(mapbox_style="open-street-map") to display an online map with map tiles.
What is a choropleth map?
A choropleth map uses color shading over geographic regions to represent statistical variables like population or GDP.
How do you create a simple linear plot in Matplotlib?
Import Matplotlib (import matplotlib.pyplot as plt), supply x and y arrays to plt.plot(), label the axes and call plt.show() to render the linear graph.
What role does Pandas play in Matplotlib plotting examples?
Pandas structures like DataFrames organize data series; Matplotlib uses DataFrame columns as x and y values for plotting.
Why are map visualizations and linear plots important in data analysis?
Map visualizations reveal geographic patterns in data, while linear plots show trends and relationships between continuous variables, aiding interpretation.