简体   繁体   English

如何 plot 一个看起来像散点的零一二维矩阵?

[英]How to plot a zero-one 2d matrix that will look like a scatter?

Might be a strange question, but I am wondering if it's possible to replace a 2d matrix made up of ones and zeros with a scatter plot of say, black dots where all the ones are but nothing for zeros:可能是一个奇怪的问题,但我想知道是否有可能用散点图 plot 替换由 1 和 0 组成的二维矩阵,其中所有的都是黑点,但没有零:

Unfortunately I don't have the best reproducible answer, but I have a 2D array made up for zeros and ones (size 275 and 357):不幸的是,我没有最好的可重现答案,但我有一个由零和一组成的二维数组(大小为 275 和 357):

I am hoping to basically cover the areas that are made up of ones with small black dots (assuming in the form of a scatter plot which will later be overlayed on another contour plot):我希望基本上覆盖由小黑点组成的区域(假设以散点图 plot 的形式,稍后将覆盖在另一个等高线图上):

The original contour plot is on the left and the idea I'm going for is on the right (picture more black dots just on the areas made up of ones):原始轮廓 plot 在左边,我想要的想法在右边(图片更多的黑点就在由 1 组成的区域上):

在此处输入图像描述

I tried making a reproducible array here:我尝试在这里制作一个可重现的数组:

#array of ones and zeros 
array = np.array(([0,0,0,0,0,0,1,1,0,0,0,1,1,1], [0,1,0,0,0,1,0,0,0,0,0,1,0,1]))

plt.pcolormesh(array)

I tried using this as an example and apply it to the 2D array, but getting some errors?我尝试以此为例并将其应用于二维数组,但出现了一些错误?

# as an example, borrowed from: https://stackoverflow.com/questions/41133419/how-to-do-the-scatter-plot-for-the-lists-or-2d-array-or-matrix-python
X=[[0,3,4,0,1,1],
    [0,0,0,5,1,1],
    [6,7,0,8,1,1],
    [3,6,1,5,6,1]]

Y=[12,15,11,10]

x_arr = np.array(X)
y = np.array(Y)

fig, ax = plt.subplots()

#colors=list('bgrcmykw')

for i, x in enumerate(x_arr.T):
    ax.scatter(x,y, c='k',s=5)

plt.show()

在此处输入图像描述

My goal is to basically convert this 2d matrix made up of ones and zeros to a scatter plot or some sort of graph where the ones are made up of black dots and the zeros have nothing.我的目标是基本上将这个由 1 和 0 组成的二维矩阵转换为散点图 plot 或某种图形,其中 1 由黑点组成,而 0 什么都没有。 This will later be overlaid on another contour plot. how might I go about setting the ones to a scatter plot made up of black dots?这稍后将覆盖在另一个轮廓 plot 上。我 go 如何将这些设置为由黑点组成的散点 plot?

Here's what I would do.这就是我会做的。 I didn't plot all the points to reduce the computational demand of creating the figure.我没有 plot 所有点来减少创建图形的计算需求。 you might want to do that if you have a lot of points to plot. either way, you can change that according to your need.如果您对 plot 有很多积分,您可能想要这样做。无论哪种方式,您都可以根据需要进行更改。

import numpy as np
from matplotlib import pyplot as plt
np.random.seed(0)
mask = np.random.randint(0, 2, (20, 20))
ys, xs = np.where(mask.astype(bool))

plt.imshow(mask)
plt.scatter(xs[::2], ys[::2])

output: output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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