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

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

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')

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

地图在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:


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:
地图在Python中使用geopandas无法正确检测颜色。

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()

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:

确定