地图在Python中使用geopandas无法正确检测颜色。

huangapple go评论113阅读模式
英文:

Map does not correctly detect colors with geopandas in Python

问题

我使用geopandas创建了一个地图,其中的颜色是根据一些条件确定的。然而,我的代码在颜色方面没有正常工作:红色、黄色和绿色。这是我尝试过的代码:

  1. def color_mapping(row):
  2. if row['pedidos_venta'] > 10000:
  3. return 'green'
  4. elif row['pedidos_venta'] > 2000:
  5. return 'yellow'
  6. else:
  7. return 'red'
  8. mapa_pedidos_venta['color'] = mapa_pedidos_venta.apply(color_mapping, axis=1)
  9. mapa_pedidos_venta.plot(column='color')

然而,我的结果是这样的:

地图在Python中使用geopandas无法正确检测颜色。

我不明白为什么颜色范围与我提供的不同。

英文:

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:

  1. def color_mapping(row):
  2. if row['pedidos_venta'] > 10000:
  3. return 'green'
  4. elif row['pedidos_venta'] > 2000:
  5. return 'yellow'
  6. else:
  7. return 'red'
  8. mapa_pedidos_venta['color'] = mapa_pedidos_venta.apply(color_mapping, axis=1)
  9. mapa_pedidos_venta.plot(column = 'color')

Nevertheless, my result is this:
地图在Python中使用geopandas无法正确检测颜色。

I don't understand why the color range is different from the one I provide

答案1

得分: 1

使用ListedColormap创建自定义颜色映射,并将其与'color'列一起绘制:

  1. import geopandas as gpd
  2. import matplotlib.pyplot as plt
  3. from matplotlib.colors import ListedColormap
  4. def color_mapping(row):
  5. if row['pedidos_venta'] > 10000:
  6. return 'green'
  7. elif row['pedidos_venta'] > 2000:
  8. return 'yellow'
  9. else:
  10. return 'red';
  11. mapa_pedidos_venta['color'] = mapa_pedidos_venta.apply(color_mapping, axis=1)
  12. mapa_pedidos_venta.plot(column='color')
  13. # create a custom color map
  14. cmap = ListedColormap(['red', 'yellow', 'green'])
  15. # plot map using 'color' column and custom color map
  16. fig, ax = plt.subplots(figsize=(10, 6))
  17. mapa_pedidos_venta.plot(column='color', cmap=cmap, ax=ax)
  18. plt.show()
英文:

Create a custom color map using the ListedColormap and plot it along with the 'color' column:

  1. import geopandas as gpd
  2. import matplotlib.pyplot as plt
  3. from matplotlib.colors import ListedColormap
  4. def color_mapping(row):
  5. if row['pedidos_venta'] > 10000:
  6. return 'green'
  7. elif row['pedidos_venta'] > 2000:
  8. return 'yellow'
  9. else:
  10. return 'red'
  11. mapa_pedidos_venta['color'] = mapa_pedidos_venta.apply(color_mapping, axis=1)
  12. mapa_pedidos_venta.plot(column = 'color')
  13. # create a custom color map
  14. cmap = ListedColormap(['red', 'yellow', 'green'])
  15. # plot map using 'color' column and custom color map
  16. fig, ax = plt.subplots(figsize=(10, 6))
  17. mapa_pedidos_venta.plot(column='color', cmap=cmap, ax=ax)
  18. plt.show()

huangapple
  • 本文由 发表于 2023年2月27日 03:36:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定