-1

What I want to achieve is to conditionally color my scatter plot. I followed this post, but I get TypeError: len() of unsized object. I'm doing the following

for i in range(n_cells):
   col = np.where(cell_list[i].m_n == 1, 'g', 'k')
   plt.scatter(cell_list[i].x, cell_list[i].y, c = col, alpha = 0.5, s = 2)

cell_list is an array of cells, which is a user-defined object. I have checked and ensured that col is an array of g and k of the correct size. Moreover, if you change c = col to c = 'b' for example, the plot works correctly. I'm relatively new to Python, so I cannot find what the issue is.

Ptheguy
  • 503
  • 4
  • 11
  • 1
    For more specific help, please post a [reprex]. – unutbu Jun 26 '19 at 20:22
  • It is hard for us to test the code, but I believe np.where always returns a numpy object. Have you tried `c = str(col)` ? This seems to work on my end. – Ludovick Bégin Jun 26 '19 at 20:27
  • The code you posted could raise that TypeError if `cell_list[i].x` is a [NumPy scalar](https://stackoverflow.com/q/21968643/190597) (such as `np.array(1)`). – unutbu Jun 26 '19 at 20:27

1 Answers1

0

Since np.where always returns a numpy object, using str(col) works for the following script.

import matplotlib.pyplot as plt
import numpy as np

col = np.where(0 == 1, 'g', 'k')
plt.scatter([0, 1], [0, 1], c = str(col), alpha = 0.5, s = 2)

plt.show()