熊猫注释的框重叠

时间:2019-06-16 23:07:23

标签: pandas matplotlib

import pandas as pd
import geopandas
import matplotlib.pyplot as plt
from pylab import rcParams


bbox_args = dict(boxstyle="round", fc="0.9", alpha = 0.8)

def plot_ip_geolocation(ips_data, figName = None):
    rcParams['figure.figsize'] = 20, 30
    df = pd.DataFrame.from_dict(ips_data)

    gdf = geopandas.GeoDataFrame(
        df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude))

    world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))

    ax = world.plot(
        color='white', edgecolor='black')

    lastLong = None
    lastLat = None
    for i, ip_data in enumerate(ips_data):
        annotation = "{}\n{}, {}".format(
            ip_data["IP Address"], 
            ip_data["City"] if 'City' in ip_data else 'Unk', 
            ip_data["Country"])
        ax.annotate(
            annotation, 
            (ip_data["Longitude"], ip_data["Latitude"]), 
            xytext=(10,-5), 
            textcoords='offset points',
            bbox = bbox_args)
        if lastLat:
            ax.arrow(lastLong, lastLat,  
                     ip_data['Longitude'] - lastLong, ip_data['Latitude'] - lastLat,
                     head_width=0.05, head_length=0.1, #fc='k', ec='k', 
                     color='red'if ip_data['Outlier'] else 'black')

        lastLat=ip_data['Latitude']
        lastLong=ip_data['Longitude']
    gdf.plot(ax=ax, color='red')

    plt.yticks([])
    plt.xticks([])

    if figName:
        plt.savefig('img/' + figName + '.jpg', bbox_inches='tight')
    plt.show()


ips_data = [{'Latitude': -34.5940475, 'Longitude': -58.4429251, 'IP Address': '200.89.165.150', 'City': 'Buenos Aires (Palermo)', 'Country': 'AR', 'Outlier': False}, {'Latitude': -34.5940475, 'Longitude': -58.4429251, 'IP Address': '200.89.165.149', 'City': 'Buenos Aires (Palermo)', 'Country': 'AR', 'Outlier': False}, {'Latitude': -34.5940475, 'Longitude': -58.4429251, 'IP Address': '200.89.165.130', 'City': 'Buenos Aires (Palermo)', 'Country': 'AR', 'Outlier': False}, {'Latitude': -34.5940475, 'Longitude': -58.4429251, 'IP Address': '200.89.165.222', 'City': 'Buenos Aires (Palermo)', 'Country': 'AR', 'Outlier': False}, {'Latitude': 41.894802, 'Longitude': 12.4853384, 'IP Address': '185.70.203.32', 'City': 'Rome', 'Country': 'IT', 'Outlier': False}, {'Latitude': 39.0437192, 'Longitude': -77.4874899, 'IP Address': '89.221.41.181', 'City': 'Ashburn', 'Country': 'US', 'Outlier': True}, {'Latitude': 25.7742658, 'Longitude': -80.1936589, 'IP Address': '154.54.9.17', 'City': 'Miami', 'Country': 'US', 'Outlier': False}, {'Latitude': 25.7742658, 'Longitude': -80.1936589, 'IP Address': '154.54.47.17', 'City': 'Miami', 'Country': 'US', 'Outlier': False}, {'Latitude': 25.7742658, 'Longitude': -80.1936589, 'IP Address': '154.54.24.145', 'City': 'Miami', 'Country': 'US', 'Outlier': False}, {'Latitude': 33.7490987, 'Longitude': -84.3901849, 'IP Address': '154.54.7.157', 'City': 'Atlanta', 'Country': 'US', 'Outlier': False}, {'Latitude': 51.5073219, 'Longitude': -0.1276474, 'IP Address': '154.54.30.186', 'City': 'London', 'Country': 'GB', 'Outlier': False}, {'Latitude': 52.3745403, 'Longitude': 4.89797550561798, 'IP Address': '154.54.56.94', 'City': 'Amsterdam', 'Country': 'NL', 'Outlier': False}, {'Latitude': 50.1106444, 'Longitude': 8.6820917, 'IP Address': '130.117.0.122', 'City': 'Frankfurt am Main', 'Country': 'DE', 'Outlier': False}, {'Latitude': 50.1106444, 'Longitude': 8.6820917, 'IP Address': '130.117.0.2', 'City': 'Frankfurt am Main', 'Country': 'DE', 'Outlier': False}, {'Latitude': 50.1106444, 'Longitude': 8.6820917, 'IP Address': '149.29.9.10', 'City': 'Frankfurt am Main', 'Country': 'DE', 'Outlier': False}, {'Latitude': 52.2034823, 'Longitude': 0.1235817, 'IP Address': '62.40.98.106', 'City': 'Cambridge', 'Country': 'GB', 'Outlier': False}, {'Latitude': 38.7077507, 'Longitude': -9.1365919, 'IP Address': '193.137.0.13', 'City': 'Lisbon', 'Country': 'PT', 'Outlier': False}, {'Latitude': 38.7077507, 'Longitude': -9.1365919, 'IP Address': '193.137.1.18', 'City': 'Lisbon', 'Country': 'PT', 'Outlier': False}]
plot_ip_geolocation(ips_data, figName = None)

这将生成以下情节:

enter image description here

如您所见,方框重叠,这是我要避免的事情。有什么想法可以解决重叠问题吗?

0 个答案:

没有答案