简体   繁体   中英

Matplotlib scatter plot of unfilled squares

I would like to make a scatter plot with unfilled squares. markerfacecolor is not an option recognized by scatter . I made a MarkerStyle but the fill style seems to be ignored by the scatter plot. Is there a way to make unfilled markers in the scatterplot?

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()

It would appear that if you want to use plt.scatter() then you have to use facecolors = 'none' instead of setting fillstyle = 'none' in construction of the MarkerStyle , eg

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

plt.show()

or, use plt.plot() with fillstyle = 'none' and linestyle = 'none' but since the marker keyword in plt.plot does not support MarkerStyle objects you have to specify the style inline, ie

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

plt.show()

either of which will give you something that looks like this

在此处输入图片说明

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

Try adding facecolors='none' to your plt.scatter

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM