【问题标题】:AttributeError: 'PathCollection' object has no property 'markeredgecolor'AttributeError:“PathCollection”对象没有属性“markeredgecolor”
【发布时间】:2021-03-03 12:51:50
【问题描述】:

我正在尝试在我的绘图上添加 markeredgecolor(marker='.' 并且我希望标记被不同的颜色包围,具体取决于它们的特征)。

我试图用地理数据这样做:https://python-graph-gallery.com/131-custom-a-matplotlib-scatterplot/

fig, ax = plt.subplots(figsize = (8,6)) 
df.plot(ax=ax,color='green', marker=".", markersize=250)
df2.plot(ax=ax,color='green', marker=".", markerfacecolor="orange", markersize=250)

但是我得到了这个错误:

AttributeError: 'PathCollection' object has no property 'markeredgecolor'

你知道问题出在哪里,该怎么办吗?

编辑 - 使用可重现的示例:

#Packages needed
import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
import shapely.wkt

#Creating GeoDataFrame df2
df2 = pd.DataFrame([shapely.wkt.loads('POINT (7.23173 43.68249)'),shapely.wkt.loads('POINT (7.23091 43.68147)')])
df2.columns=['geometry']
df2 = gpd.GeoDataFrame(df2)
df2.crs = {'init' :'epsg:4326'}

#Ploting df2
fig, ax = plt.subplots()
df2.plot(ax=ax,color='green', marker=".", markerfacecolor="orange")

【问题讨论】:

  • 我认为你的代码是pandas提供的绘图功能,它没有标记边缘的颜色。你指的是maplotlib。
  • 您使用的是什么版本的 pandas 和 matplotlib?这对我有用
  • 感谢您的回答! @r-beginners 你的意思是我没有在这里使用 matplotlib,对吧? (我不明白为什么有两种方法可以在 Python 上做图,一定是这样!)
  • @DavidG 我在 pandas 1.1.3 和 matplotlib 3.3.1 上。你呢?
  • 这种版本组合也适用于我。你能创建一个minimal reproducible example吗?

标签: python matplotlib marker


【解决方案1】:

Geopandas plot 不接受所有参数作为 matplotlib.plot(参考 [此处])(https://geopandas.readthedocs.io/en/latest/docs/reference/api/geopandas.GeoDataFrame.plot.html

但是,有一些 **style_kwds 可以为您完成工作(虽然文档中没有明确解释):

收回你的代码

df2.plot(
    ax=ax,
    color='green',
    marker=".",
    markersize=1000,
    edgecolor='orange', # this modifies the color of the surrounding circle
    linewidth=5 # this modifies the width of the surrounding circle
)

requirements.txt中有这个配置

geopandas~=0.9.0
matplotlib~=3.3.4
pandas~=1.2.3
shapely~=1.7.1

【讨论】:

  • 嗯,我收到了这个错误:/“AttributeError: 'PathCollection' object has no property 'kind'”
  • 刚刚用相应版本的包编辑了答案;-)
猜你喜欢
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 2012-12-01
  • 2021-04-19
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
相关资源
最近更新 更多