How do I apply a line between two points in geopanda e.g. between 2 cities

Viewed 1524

I am trying to plot a green line between the 2 cities on my map in geopandas The result should show the 2 cities with the red point and the name of the cities as well plus a green line between the two cities

I hope you can help me! Thanks in ahead!

I tried it a few times but I dont get the key for plotting a line

import geopandas as gpd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


###################### The Map of Germany is plotted here

plt.style.use('seaborn')
plz_shape_df = gpd.read_file(r'C:\Users\XXXXX\geopanda\plz-gebiete.shp', dtype={'plz': str})
plz_shape_df.head()
plt.rcParams['figure.figsize'] = [16, 11]
fig, ax = plt.subplots()
plz_shape_df.plot(ax=ax, color='orange', alpha=0.8)
 
ax.set(
       title='Germany', 
       aspect=1.3, 
       facecolor='lightblue');

################ The dict: new_dict3 with the 2 cities gets plotted

new_dict3 = {
    'Stuttgart': (9.181332, 48.777128),
    'Munich': (11.576124, 48.137154),
    }

for c in new_dict3.keys():
   ax.text(
       x=new_dict3[c][0],
       y=float(new_dict3[c][1]) + 0.1,
       s=c,
       fontsize = 12,
       ha = "center",
       )
        
   ax.plot(
        new_dict3[c][0],
        new_dict3[c][1],
        marker = "o",
        c = "red",
        alpha = 2.0
        )

############### Now I want to plot a green line between the 2 cities of the new_dict3

   ax.plot(
        x= new_dict3[c][0],
        y= float(new_dict3[c][1]) + 0.1,
        linestyle = "--",
        c = "green",
        marker="",
        )

    #this doesn't work
    

[the result of plotting the map before trying to plot the line

1 Answers

I got the right answer myself here is my result:


Stuttgart = [9.181332, 48.777128] 
Munich = [11.576124, 48.137154]

x_values = [ Stuttgart[0], Munich[0]]
y_values = [ Stuttgart[1], Munich[1]]

plt.plot(x_values, y_values, linewidth = 5, linestyle = "--", color = "green")

Related