Search code examples
pythonpython-3.xpandasmatplotlibscatter-plot

How to annotate data on the scatter plot (geo map)?


From california house dataframe (ch),

I created this code:

lat, lon = ch['latitude'], ch['longitude']
mhv = ch['median_house_value']
# Scatter the points, using size and color but no label
plt.figure(figsize=(10,7))
plt.scatter(
    lon, lat, 
    c=mhv, 
    s=ch['population']/100, 
    linewidth=0, alpha=0.9,
)
plt.xlabel('longitude')
plt.ylabel('latitude')
plt.colorbar(label='median house value')

for pop in [5, 10, 50, 100, 200]:
    plt.scatter([], [],  alpha=0.6, s=pop,
                label=str(pop), c='k')
plt.legend(scatterpoints=1, frameon=False,
           labelspacing=1, title='population size* 10e-2')

plt.show()

It gives me this figure: enter image description here

The question is: how do I note an arrow and parenthesis with the min and max coordinates for both latitude and longitude, on the map?

In other words: I want this -->(min lat, min lon) and -->(max lat, max lon) to mark the spots on the map.


Solution

  • I think your data is from Kaggle, but I do not have it. Anyway, maybe you can try these:

    # add text at bottom left
    x,y = (-123,33)
    plt.text(x,y,'(min lat, min lon)',fontsize=20,weight='bold',bbox=dict(facecolor='white', edgecolor='black'))
    
    # add arrows (use negative values to reverse the arrow direction)
    plt.arrow(x=-124, y=33, dx=5, dy=0, width=.08, color = "purple") 
    

    Of course, you may adjust the input values based on your needs.