Skip to content

Instantly share code, notes, and snippets.

View hbshrestha's full-sized avatar

Himalaya hbshrestha

  • Bonn, Germany
  • 09:37 (UTC +02:00)
View GitHub Profile
@hbshrestha
hbshrestha / gist:356aebdd5536577d3616850479debbe2
Created December 15, 2023 20:42
visualizing trade flow in Python maps Part I
ax = plt.axes(projection=ccrs.PlateCarree())
# get country centroids
ax.set_extent([lon[0] - 1, lon[1] + 1, lat[0] - 1, lat[1] + 1])
for key, cn in zip(c.keys(),c.values()):
ax.add_geometries(cn.geometry,
crs=ccrs.PlateCarree(),
edgecolor="grey",
facecolor="whitesmoke",
@hbshrestha
hbshrestha / update_logbook.bas
Created August 20, 2023 21:19
ProjectTasksTracker
Dim i, j, lr2 As Integer
Dim rg1, rg2 As Range
Dim k As Long
'Loop through each row of ProjectTasksTracker sheet except the header row
For i = 2 To lr1
'Check if the update column is not empty in ProjectTasksTracker sheet.
'Proceed if not empty
If ws1.Cells(i, 6).Value <> "" Then
import os, json, requests
#all sky surface shortwave downward irradiance
radiation_parameter = "ALLSKY_SFC_SW_DWN"
#url for irradiance
base_url = r"https://power.larc.nasa.gov/api/temporal/hourly/point?parameters=ALLSKY_SFC_SW_DWN&community=RE&time-standard=UTC&longitude={longitude}&latitude={latitude}&format=JSON&start=2020&end=2020"
rad_data = []
rescale = lambda y: (y - np.min(y)) / (np.max(y) - np.min(y))
fig, ax1 = plt.subplots()
x = df_temp.index.tolist()
y = df_temp["mean"].values.tolist()
ax1.bar(x, y, color = my_cmap(rescale(y)), width = 0.8)
ax1.set_ylabel("Temperature anomaly \n relative to 1951-80 mean (°C)", color = "red")
ax1.tick_params(axis='y', color='red', labelcolor='red')
gases = ["CO$_2$", "CH$_4$", "N$_2$O", "F-gases"]
warming_potential = [1, 25, 300, 1000]
text_height = [i*1.2 for i in warming_potential]
text = ["1", "25", "300", "1000+"]
volume = [74.4, 17.3, 6.2, 2.1]
colors = ["brown", "darkslategray", "darkgray", "purple"]
fig, (ax1, ax2) = plt.subplots(1, 2)
@hbshrestha
hbshrestha / global_surface_temperature_anomaly
Created October 4, 2022 12:11
code to get the global surface temperature anomaly using NASA GISSTEMP v4 data
fig, ax = plt.subplots()
df["Annual Mean"].plot(ax = ax, c = "black", marker = "s")
df["Lowess Smoothing"].plot(ax = ax, c = "red")
x = df.index.tolist()
y = df["Annual Mean"].tolist()
# Define the 95% confidence interval
ci = np.mean(y) + 1.96 * np.std(y) / np.sqrt(len(y))
plt.fill_between(x, y-ci, y+ci,
@hbshrestha
hbshrestha / warming_stripes
Created October 3, 2022 19:57
code to create warming stripes
anomaly = df["Annual Mean"]
fig = plt.figure(figsize = (10, 1.5))
ax = fig.add_axes([0, 0, 1, 1])
#turn the x and y-axis off
ax.set_axis_off()
#create a collection with a rectangle for each year
col = PatchCollection([
@hbshrestha
hbshrestha / folium map
Created June 18, 2022 22:27
folium map for TSP solution for Germany
import folium
folium_coordinates = []
for x,y in coordinates:
folium_coordinates.append([y,x])
route = []
for stop in cycle:
route.append(folium_coordinates[stop])
@hbshrestha
hbshrestha / tree of Hamilton paths
Last active June 23, 2022 04:53
Return the tree structure of Hamilton paths
import networkx as nx
import matplotlib.pyplot as plt
cities = ["Berlin","Dusseldorf","Hamburg","Munich","Berlin"]
def make_tsp_tree(cities):
"""
Create all Hamilton paths from start to end city from a list of cities.
Creates a directed prefix tree from a list of the created paths.
Remove the root node and nil node.
@hbshrestha
hbshrestha / waterfall chart 2
Created May 16, 2022 14:27
waterfall chart 2
fig = go.Figure()
fig.add_trace(go.Waterfall(x = [["Initial","Short-term measure","Short-term measure",
"Short-term measure","Short-term measure","Short-term measure",
"Intermediate", "Long-term measure","Long-term measure",
"Long-term measure","Final"],
df["Values"]],
y = df["kWh"],
measure = df["measure"].tolist(),
base = 0, #by default