Cartopy/Matplotlib RGBA错误

Cartopy/Matplotlib RGBA错误,matplotlib,cartopy,Matplotlib,Cartopy,我正在尝试使用cartopy和matplotlib在地图上使用自定义颜色绘制地图 import numpy as np import matplotlib.pyplot as plt import cartopy as cartopy import pandas as pd import random as rd def getcolor(buurtnaam): a = rd.uniform(0.0, 255.0) b = rd.uniform(0.0, 255.0)

我正在尝试使用cartopy和matplotlib在地图上使用自定义颜色绘制地图

import numpy as np
import matplotlib.pyplot as plt
import cartopy as cartopy
import pandas as pd
import random as rd

def getcolor(buurtnaam):
    a = rd.uniform(0.0, 255.0)
    b = rd.uniform(0.0, 255.0)
    c = rd.uniform(0.0, 255.0)
    return tuple([a, b, c, 1])

ax = plt.axes(projection=cartopy.crs.PlateCarree())
ax.set_extent((5.35, 5.60, 51.4, 51.5), crs=cartopy.crs.PlateCarree())
filelocation=('buurt.shp')

reader = cartopy.io.shapereader.Reader(filelocation)

for label,shape in zip(reader.records(),reader.geometries()):
    coordinates=cartopy.feature.ShapelyFeature(shape, cartopy.crs.PlateCarree(),edgecolor='black')
    ax.add_feature(coordinates, facecolor=getcolor(label.attributes['buurtnaam']))
plt.show()
但是,这会产生以下结果:

ValueError:无效的RGBA参数:5.85057504984446

当我通过在for循环中打印RGBA值来检查RGBA值时,它们看起来是正确的

print(label.attributes['buurtnaam'])
罗丘斯布特

(109.8833008320893179.51867989390442211.09771601504892,1)

类“tuple”


我的RGBA格式正确吗?这是cartopy/matplotlib中的错误吗?

看起来您的
get\u color
函数正在生成与shapefile无关的随机数,这些数字正用于RGB值。当您将属性名
buurtnaam
传递给函数时,您需要以某种方式将其合并到RGB值生成中。

同时我解决了它。RGBA元组应包含4个介于0和1之间的值

import numpy as np
import matplotlib.pyplot as plt
import cartopy as cartopy
import pandas as pd
import random as rd

def getcolor(buurtnaam):
    a = rd.uniform(0.0, 1.0)
    b = rd.uniform(0.0, 1.0)
    c = rd.uniform(0.0, 1.0)
    return tuple([a, b, c, 1])

ax = plt.axes(projection=cartopy.crs.PlateCarree())
ax.set_extent((5.35, 5.60, 51.4, 51.5), crs=cartopy.crs.PlateCarree())
filelocation=('buurt.shp')

reader = cartopy.io.shapereader.Reader(filelocation)

for label,shape in zip(reader.records(),reader.geometries()):
    coordinates=cartopy.feature.ShapelyFeature(shape, cartopy.crs.PlateCarree(),edgecolor='black')
    ax.add_feature(coordinates, facecolor=getcolor(label.attributes['buurtnaam']))
plt.show()

我这样做是为了创建一个清晰的Mnimal工作示例。在实践中,我将用一个基于形状标签将不同属性组合成颜色的函数替换此函数。
print (type(getcolor(label.attributes['buurtnaam'])))
import numpy as np
import matplotlib.pyplot as plt
import cartopy as cartopy
import pandas as pd
import random as rd

def getcolor(buurtnaam):
    a = rd.uniform(0.0, 1.0)
    b = rd.uniform(0.0, 1.0)
    c = rd.uniform(0.0, 1.0)
    return tuple([a, b, c, 1])

ax = plt.axes(projection=cartopy.crs.PlateCarree())
ax.set_extent((5.35, 5.60, 51.4, 51.5), crs=cartopy.crs.PlateCarree())
filelocation=('buurt.shp')

reader = cartopy.io.shapereader.Reader(filelocation)

for label,shape in zip(reader.records(),reader.geometries()):
    coordinates=cartopy.feature.ShapelyFeature(shape, cartopy.crs.PlateCarree(),edgecolor='black')
    ax.add_feature(coordinates, facecolor=getcolor(label.attributes['buurtnaam']))
plt.show()