英文:
Map does not correctly detect colors with geopandas in Python
问题
我使用geopandas创建了一个地图,其中的颜色是根据一些条件确定的。然而,我的代码在颜色方面没有正常工作:红色、黄色和绿色。这是我尝试过的代码:
def color_mapping(row):
if row['pedidos_venta'] > 10000:
return 'green'
elif row['pedidos_venta'] > 2000:
return 'yellow'
else:
return 'red'
mapa_pedidos_venta['color'] = mapa_pedidos_venta.apply(color_mapping, axis=1)
mapa_pedidos_venta.plot(column='color')
然而,我的结果是这样的:
我不明白为什么颜色范围与我提供的不同。
英文:
I'm creating a map with geopandas whose colors are determinated by some conditions. However, my code is not working properly in relation to colors: red, yellow and green. This that I've tried:
def color_mapping(row):
if row['pedidos_venta'] > 10000:
return 'green'
elif row['pedidos_venta'] > 2000:
return 'yellow'
else:
return 'red'
mapa_pedidos_venta['color'] = mapa_pedidos_venta.apply(color_mapping, axis=1)
mapa_pedidos_venta.plot(column = 'color')
Nevertheless, my result is this:
I don't understand why the color range is different from the one I provide
答案1
得分: 1
使用ListedColormap
创建自定义颜色映射,并将其与'color'列一起绘制:
import geopandas as gpd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
def color_mapping(row):
if row['pedidos_venta'] > 10000:
return 'green'
elif row['pedidos_venta'] > 2000:
return 'yellow'
else:
return 'red';
mapa_pedidos_venta['color'] = mapa_pedidos_venta.apply(color_mapping, axis=1)
mapa_pedidos_venta.plot(column='color')
# create a custom color map
cmap = ListedColormap(['red', 'yellow', 'green'])
# plot map using 'color' column and custom color map
fig, ax = plt.subplots(figsize=(10, 6))
mapa_pedidos_venta.plot(column='color', cmap=cmap, ax=ax)
plt.show()
英文:
Create a custom color map using the ListedColormap
and plot it along with the 'color' column:
import geopandas as gpd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
def color_mapping(row):
if row['pedidos_venta'] > 10000:
return 'green'
elif row['pedidos_venta'] > 2000:
return 'yellow'
else:
return 'red'
mapa_pedidos_venta['color'] = mapa_pedidos_venta.apply(color_mapping, axis=1)
mapa_pedidos_venta.plot(column = 'color')
# create a custom color map
cmap = ListedColormap(['red', 'yellow', 'green'])
# plot map using 'color' column and custom color map
fig, ax = plt.subplots(figsize=(10, 6))
mapa_pedidos_venta.plot(column='color', cmap=cmap, ax=ax)
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论