【问题标题】:Matplotlib scatter plot of unfilled squares未填充正方形的 Matplotlib 散点图
【发布时间】:2019-12-25 10:51:57
【问题描述】:

我想用未填充的正方形制作散点图。 markerfacecolor 不是 scatter 识别的选项。我做了一个MarkerStyle,但散点图似乎忽略了填充样式。有没有办法在散点图中制作未填充的标记?

import matplotlib.markers as markers
import matplotlib.pyplot as plt 
import numpy as np

def main():
    size = [595, 842] # in pixels
    dpi = 72. # dots per inch
    figsize = [i / dpi for i in size]
    fig = plt.figure(figsize=figsize)
    ax = fig.add_axes([0,0,1,1])

    x_max = 52
    y_max = 90
    ax.set_xlim([0, x_max+1])
    ax.set_ylim([0, y_max + 1]) 

    x = np.arange(1, x_max+1)
    y = [np.arange(1, y_max+1) for i in range(x_max)]

    marker = markers.MarkerStyle(marker='s', fillstyle='none')
    for temp in zip(*y):
        plt.scatter(x, temp, color='green', marker=marker)

    plt.show()

main()

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    如果你想使用plt.scatter(),那么你必须使用facecolors = 'none'而不是在MarkerStyle的构造中设置fillstyle = 'none',例如

    marker = markers.MarkerStyle(marker='s')
    for temp in zip(*y):
        plt.scatter(x, temp, color='green', marker=marker, facecolors='none')
    
    plt.show()
    

    或者,将plt.plot()fillstyle = 'none'linestyle = 'none' 一起使用,但由于plt.plot 中的marker 关键字不支持MarkerStyle 对象,您必须内联指定样式,即

    for temp in zip(*y):
        plt.plot(x, temp, color='green', marker='s', fillstyle='none')
    
    plt.show()
    

    其中任何一个都会给你看起来像这样的东西

    【讨论】:

      【解决方案2】:

      参考:How to do a scatter plot with empty circles in Python?

      尝试将facecolors='none' 添加到您的plt.scatter

      plt.scatter(x, temp, color='green', marker=marker, facecolors='none')
      

      【讨论】:

      • 啊,是的。分散文档的链接。我错过了。谢谢。
      • 对不起,我的错误,我离开了旧版本的页面,该页面仍然在文档中列出了 facecolors kwarg,似乎他们进行了一些编辑。
      猜你喜欢
      • 1970-01-01
      • 2017-04-05
      • 2012-08-07
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 2015-06-07
      相关资源
      最近更新 更多