Global Disease Outbreak (June 2024) #850
Replies: 3 comments 1 reply
-
NOTE: The final figure in the submission uses matplotlib plus basemap. The first try with plotnine did not go all the way. This is the plotnine part with the resulting figure. import pandas as pd
import geopandas as gpd
import contextily as ctx
import matplotlib.pyplot as plt
from plotnine import ggplot, aes, geom_point, theme_minimal, labs, scale_color_brewer, scale_size
# Load the data
data = pd.read_csv('world_disease_data.csv')
df = pd.DataFrame(data)
# Convert to GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df['Lng'], df['Lat']))
# Set the coordinate reference system (CRS) to EPSG:4326 (WGS 84)
gdf.set_crs(epsg=4326, inplace=True)
# Reproject to EPSG:3857
gdf = gdf.to_crs(epsg=3857)
# Convert GeoDataFrame to Pandas DataFrame for Plotnine
gdf['x'] = gdf.geometry.x
gdf['y'] = gdf.geometry.y
# Create a 'Plotnine' plot for visualization
plot = (
ggplot(gdf) +
aes(x='x', y='y', color='Disease', size='Rating') +
geom_point(alpha=0.6) +
theme_minimal() +
labs(title='Disease Outbreaks', x='Longitude', y='Latitude', color='Disease', size='Rating') +
scale_color_brewer(type='qual', palette='Dark2') +
scale_size(range=(2, 10))
)
plot It was possible to continue using plotnine plus basemap. from plotnine import *
plot2 = (
plot # from above
+ guides(
color=guide_legend(
theme=theme(
legend_position=(0.965, 0.97),
)
),
size=guide_legend(
theme=theme(
legend_position=(0.03, 0.03),
)
)
)
+ theme(
figure_size=(15, 10),
plot_background=element_rect(fill="white"),
panel_border=element_rect(color="black"),
legend_margin=5,
legend_background=element_rect(fill="#FFFFFFBB", color="#00000022"),
axis_ticks=element_blank(),
axis_text=element_blank(),
axis_title=element_blank(),
plot_margin_top=0.2,
)
)
fig = plot2.draw()
ax = fig.get_axes()[0]
ctx.add_basemap(ax, crs=gdf.crs.to_string(), source=ctx.providers.OpenStreetMap.Mapnik)
fig |
Beta Was this translation helpful? Give feedback.
-
Ahoy @nimisha-18, |
Beta Was this translation helpful? Give feedback.
-
@nimisha-18, The winning submission has been announced here. Thank you for taking part in the contest. |
Beta Was this translation helpful? Give feedback.
-
Authors
Nimisha Singh
Links
[GitHub] (https://github.com/nimisha-18), [LinkedIn] (www.linkedin.com/in/nimisha-singh-859015244)
Full description
Plotnine Contest 2024
This project visualizes global disease outbreaks using Plotnine (R's visualization grammar for Python), GeoPandas, and Contextily. The map plots the locations of various disease outbreaks with color-coded points indicating the type of disease and the size representing the severity rating. The addition of a basemap from OpenStreetMap enhances the geographical context. This repository includes the code and dataset used to create the visualization, demonstrating techniques in geospatial data handling and visualization.
Code repository
https://github.com/nimisha-18/Plotnine_contest_2024
Beta Was this translation helpful? Give feedback.
All reactions