在使用pandas进行csv-fi时,如何更改不同数据类型的标记样式

2024-04-26 03:34:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个csv文件,里面有100gb的地方的数据,列有它们的名字、人口、类型(城镇或城市)、纬度和经度。我把它们绘制在一张经纬度地图上,标记大小和人口成比例,颜色取决于国家。我正在努力想办法改变马克笔的风格。理想的情况下,我希望有^的城镇和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, )

到目前为止,我已经尝试了标记样式相同的方式,我改变了颜色,但这在一个空图表的结果。任何帮助都将不胜感激。你知道吗


Tags: columnscsvinimportfordataasplace
1条回答
网友
1楼 · 发布于 2024-04-26 03:34:14

首先是一些虚拟数据:

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

相关问题 更多 >