我的代码当前输出的可视化效果如下所示:
我希望能够添加一个特征,它有指向每个位置点的箭头,其中每个箭头都有一个标签名称。基本上,我希望得到如下所示的输出:
我想让程序自动选择一个箭头间距,这样标签名称(如“A”,“B:等)就不会相互冲突/相互冲突。这有可能自动做到吗?
下面是我目前为止显示第一个图像的代码:
import pandas as pd
import matplotlib.pyplot as plt
from shapely.geometry import Point
import geopandas as gpd
from geopandas import GeoDataFrame
import json
#This function creates the pandas dataframe where each row is the (latitude, longitude) of a location. The first column is latitude, and the second column is longitude
def createLatitudeLongitudeDataFrame(inputJsonFolder, inputJsonFilename):
data = json.load(open(inputJsonFolder + "\\" + inputJsonFilename))
latitudes = []
longitudes = []
for location in data["locations"]:
latitudes.append(location["coordinates"]["latitude"])
longitudes.append(location["coordinates"]["longitude"])
data_tuples = list(zip(latitudes, longitudes))
df = pd.DataFrame(data_tuples, columns=["Latitudes", "Longitudes"])
return df
#This function creates the map of the locations based on the dataframe.
def createMapOfLocations(df):
geometry = [Point(yx) for yx in zip(df['Longitudes'], df['Latitudes'])]
gdf = GeoDataFrame(df, geometry=geometry)
#This is a simple map that goes with geopandas
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
gdf.plot(ax=world.plot(figsize=(10, 6)), marker='o', color='red', markersize=15)
plt.show()
def main():
inputJsonFolder = "\\inputFolder"
inputJsonFilename = "testing.json"
df = createLatitudeLongitudeDataFrame(inputJsonFolder, inputJsonFilename)
createMapOfLocations(df)
main()
1条答案
按热度按时间gc0ot86w1#
下面的数据和代码无法满足您的所有要求。它显示了您需要的数据和在Map上绘制注解文本的代码。当Map上有许多文本时,很难在不发生冲突的情况下正确放置它们。一个可以在所有情况下对它们进行排列以获得完美结果的程序既长又复杂。
输入数据:
代码: