【问题标题】:Is it possible to change the color of an object in cartopy after it has been added?添加后是否可以更改cartopy中对象的颜色?
【发布时间】:2018-11-17 23:03:38
【问题描述】:

问题

我正在使用cartopy 在 Python 中绘制一些国家/地区。为此,我使用了这样的add_geometry 函数:

geo = ax.add_geometries(geometry,facecolor='ghostwhite', edgecolor='black',crs=data_transf)

我希望能够更改已添加的几何对象的颜色。通过这种方式,我可以在国家/地区更改颜色的情况下制作动画,而无需每帧重绘所有内容。例如,所有国家/地区都会从白色开始,然后依次变为蓝色。

但是,我似乎找不到在添加对象后更改其颜色的方法。

通常有效的方法

在 maplotlib 中使用通常的情节我会这样做:

import matplolib.pyplot as plt

line, = plt.plot([1,2,3],[4,5,6],'red') #Plot in red
line.set_color('green') #Change the color to green

为什么它在这里不起作用

FeatureArtistfrom cartopy 添加add_geometries 没有set_colorfunction。事实上,color,facecolor,... 属性似乎甚至不存在于 FeatureArtist 中,如对 matplotlib.artist.getp 的调用所示。

对于情节,调用返回:

>>import matplotlib.artist as mart
>>mart.getp(line)
agg_filter = None
alpha = None
animated = False
antialiased = True
children = []
clip_box = TransformedBbox(     Bbox(x0=0.0, y0=0.0, x1=1.0, ...
clip_on = True
clip_path = None
color = (1.0, 0.0, 0.0, 1.0)
contains = None
dash_capstyle = butt
dash_joinstyle = round
data = (array([1, 2, 3]), array([4, 5, 6]))
drawstyle = default
figure = Figure(640x476)
fillstyle = full
gid = None
in_layout = True
label = _line0
linestyle = -
linewidth = 1.5
marker = None
markeredgecolor = (1.0, 0.0, 0.0, 1.0)
markeredgewidth = 1.0
markerfacecolor = (1.0, 0.0, 0.0, 1.0)
markerfacecoloralt = none
markersize = 6.0
markevery = None
path = Path(array([[ 1.,  4.],        [ 2.,  5.],        ...
path_effects = []
picker = None
pickradius = 5
rasterized = None
sketch_params = None
snap = None
solid_capstyle = projecting
solid_joinstyle = round
transform = CompositeGenericTransform(     TransformWrapper(  ...
transformed_clip_path_and_affine = (None, None)
url = None
visible = True
xdata = [1 2 3]
xydata = [[ 1.  4.]  [ 2.  5.]  [ 3.  6.]]
ydata = [4 5 6]
zorder = 2

对于几何:

>>mart.getp(geo)
agg_filter = None
alpha = None
animated = False
children = []
clip_box = TransformedBbox(     Bbox(x0=0.0, y0=0.0, x1=1.0, ...
clip_on = True
clip_path = None
contains = None
figure = Figure(1200x990)
gid = None
in_layout = True
label = 
path_effects = []
picker = None
rasterized = None
sketch_params = None
snap = None
transform = CompositeGenericTransform(     TransformWrapper(  ...
transformed_clip_path_and_affine = (None, None)
url = None
visible = True
zorder = 1

如您所见,我无法更改 colorproperty(或类似的)。

【问题讨论】:

    标签: python-3.x cartopy


    【解决方案1】:

    这绝对是 Cartopy 界面所缺少的。你介意在 Cartopy issue tracker 上打开一个问题吗?

    作为一种解决方法,您可以修改一个 private 属性_kwargs,这似乎可行:

    import cartopy.feature as cfeature
    f = ax.add_feature(cfeature.STATES)
    f._kwargs['edgecolor'] = 'blue'
    

    注意:这是使用私有 API,因此可能会在没有任何警告的情况下更改/中断。这只是一种让您行动起来的解决方法,但我强烈建议您报告此问题,以便将适当的解决方案添加到 Cartopy。

    【讨论】:

    • 谢谢。我在问题跟踪器上打开了一个问题(#1226 供未来的读者使用)
    猜你喜欢
    • 1970-01-01
    • 2020-11-07
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多