...
x,y,reg = (np.array(v,dtype=t) for v, t in zip(
zip(*(l.split() for l in open('UK.csv'))),
(float, float, int)))
for n in set(reg):
plt.scatter(x[reg==n], y[reg==n], label=str(n))
plt.legend(title='Region')
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
# Read CSV file into a pandas DataFrame
df = pd.read_csv('uk_cities.csv')
# Dictionary to map each region to a unique color
region_color_map = {
region_1: 'blue',
region_2: 'green',
region_3: 'red' # U can add more as per requirement
}
# Create a new column in the DataFrame with the assigned color as per the region
df['color'] = df['city'].map(region_color_map)
# Scatterplot the colored points
plt.scatter(df['latitude'], df['longitude'], c=df['color'])
# Add a legend with the color codes and region names
plt.legend(handles=[plt.scatter([], [], c=color, label=region) for region, color in region_color_map.items()])
plt.show()
2条答案
按热度按时间vsaztqbk1#
UK.csv
如下下面的代码
生产
ht4b089n2#
由于没有给出数据集的描述。我假设:
下面是相同的代码片段。
上面的代码导致下面的散点图。
为便于解释;我的CSV文件只有3列-(城市,纬度,&经度)