Matplotlib散点图的散点图

时间:2019-12-25 00:02:09

标签: python matplotlib

我想用未填充的正方形绘制散点图。 scatter无法识别markerfacecolor。我做了一个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()

2 个答案:

答案 0 :(得分:2)

看来,如果要使用plt.scatter(),则必须使用facecolors = 'none'而不是在fillstyle = 'none'的构造中设置MarkerStyle,例如

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'一起使用,但是由于marker中的plt.plot关键字不支持MarkerStyle对象,指定内联样式,即

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

plt.show()

这两种方法都会给您带来如下效果

enter image description here

答案 1 :(得分:1)

引用: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')
相关问题