Как изменить стиль маркера для разных типов данных при использовании панд для файла CSV - PullRequest
0 голосов
/ 02 ноября 2018

У меня есть CSV-файл с данными для 100 ГБ мест со столбцами для их имени, населения, типа (города или города), широты и долготы. Я нанес их на карту долготы против широты с размерами маркеров, пропорциональных населению и цвету в зависимости от нации. Я изо всех сил пытаюсь найти способ изменить стиль маркера. В идеале я хотел бы иметь ^ для городов и v для городов. Вот мой код.

# imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

# import data file
# select data columns needed
data = pd.read_csv('GBplaces.csv', sep = ',', usecols = (0,1,2,3,4))
# name data columns
data.columns = ('Place','Type','Population','Latitude','Longitude')#



# make markers for towns and cities from different nations different colours
# Scotland in blue
data.loc[(data['Place'] == 'Aberdeen') | (data['Place'] == 'Dundee') | 
(data['Place'] == 'Glasgow') 
| (data['Place'] == 'Edinburgh'),'Colour'] = 'b'

# Wales in black
data.loc[(data['Place'] == 'Swansea') | (data['Place'] == 'Cardiff') | 
(data['Place'] == 'Newport'),'Colour'] = 'black'

# England in red
data.loc[(data['Place'] != 'Aberdeen') & (data['Place'] != 'Dundee') 
& (data['Place'] != 'Glasgow') & (data['Place'] != 'Edinburgh') 
& (data['Place'] != 'Swansea') & (data['Place'] != 'Cardiff') & 
(data['Place'] != 'Newport'),'Colour'] = 'r'

# legend created for colours for each nation
red_marker = mpatches.Patch(color='r',label='England')
blue_marker = mpatches.Patch(color='b', label='Scotland')
black_marker = mpatches.Patch(color='black', label='Wales')
legend = plt.legend(handles=[red_marker, blue_marker, black_marker])

# colour added to background
ax = plt.gca()
ax.patch.set_facecolor('#CCFFFF')

# make point size proportional to population
area = data['Population']/100000

plt.scatter(data['Longitude'], data['Latitude'], c = data['Colour'], s = 
area, )

До сих пор я пытался изменить стиль маркера так же, как менял цвет, но в результате получается пустой график. Любая помощь будет высоко ценится.

1 Ответ

0 голосов
/ 02 ноября 2018

Первые фиктивные данные:

df = pd.DataFrame(data={
    'Place': ['Scotland', 'Scotland', 'England', 'England', 'Wales', 'Wales'], 
    'x': [100, 90, 80, 70, 60, 50], 
    'y': [10, 20, 30, 40, 50, 60]
})

Сгруппируйте по Place и создайте список markers, а затем переберите его. В вашем случае Place будет городом или городом.

from itertools import cycle

ax = plt.gca()
ax.patch.set_facecolor('#FFFFFF')

places = df.groupby('Place')

markers = ['o', '1', ',']

legend_labels = []

for (name, place), marker in zip(places, cycle(markers)):

    ax.scatter(place.x, place.y, marker=marker)

    legend_labels.append(name)

ax.legend(labels=legend_labels)

plt.show()

enter image description here

...