1

Elaborated question:

Let me clarify my question. I want to plot a list of array output as a 2D scatter plot with polarity along x axis subjectivity along y axis and modality values that ranges between -1 and 1 determines the type of marker( o,x, ^, v)

output

polarities:  [ 0.  0.  0.  0.]
subjectivity:  [ 0.1  0.   0.   0. ]
modalities:  [ 1.   -0.25  1.    1.  ]

The modified code with limited marker value for 2 range.

print "polarities: ", a[:,0]
print "subjectivity: ", a[:,1]
print "modalities: ", a[:,2]

def markers(r):
    markers = np.array(r, dtype=np.object)
    markers[(r>=0)] = 'o'
    markers[r<0] = 'x'
    return markers.tolist()

def colors(s):
    colors = np.array(s, dtype=np.object)
    colors[(s>=0)] = 'g'
    colors[s<0] = 'r'
    return colors.tolist()

fig=plt.figure()
ax=fig.add_subplot(111)
ax.scatter(a[:,0], a[:,1], marker = markers(a[:,2]), color= colors(a[:,0]), s=100, picker=5)

My intent is to check the modality value and return one of the four markers. if I hardcore 'o' it returns the plot.

 ax.scatter(a[:,0], a[:,1], marker = markers('o'), color= colors(a[:,0]), s=100, picker=5)

As a trial i tried to mimic the color function and pass it as a[:,2] but hit a shell output error

ValueError: Unrecognized marker style ['o', 'x', 'o', 'o']

The question is: Is my approach wrong? or how to make it recognize the marker style?

Edit1

Trying to get the m value between 0 and .5

with this code

ax.scatter (p[0<m<=.5], s[0<m<=.5], marker = "v", color= colors(a[:,0]), s=100, picker=5)

yields this error

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

How to range m value between 0 and .5 in the example given in answer 2.

2 Answers 2

1

It's not clear from your question, but I assume your array a is of shape (N,3) and so your arrays s and r are actual arrays and not scalars.

First off, you cannot have several markers with one call of scatter(). If you want your plot to have several markers, you'll have to slice your array correctly and do several scatter() for each of your markers.

Regarding the colors, your problem is that your function colors(r) only return one color where it should return an array of colors (with the same number of elements as a[:,0]). Like such:

def colors(s):
    colors = np.array(s, dtype=np.object)
    colors[(s>0.25)&(s<0.75)] = 'g'
    colors[s>=0.75] = 'b'
    colors[s<=0.25] = 'r'
    return colors.tolist()

a = np.random.random((100,))
b = np.random.random((100,))

plt.scatter(a,b,color=colors(b))

ANSWER TO YOUR EDIT 1:

You seem to be on the right track, you'll have to do as many scatter() calls as you have markers.

Your error comes from the slicing index [0<m<=.5] which you cannot use like that. You have to use the full notation [(m>0.)&(m<=.5)]

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Please see elaborate question.
@Programmer_nltk I've answered your EDIT 1 problem. I believe you have everything you need to solve your problem from here
1

As Diziet pointed out, plt.scatter() cannot handle several markers. You therefore need to make one scatter plot per marker-category. This can be done my conditioning on the property which should be reflected by the marker. In this case:

import numpy as np
import matplotlib.pyplot as plt

p = np.array( [ 0. ,  0.2 ,  -0.3 ,  0.2] )
s = np.array( [ 0.1,  0.,   0.,   0.3 ] )
m = np.array( [ 1.,   -0.25,  1. ,  -0.6  ] )


colors = np.array([(0.8*(1-x), 0.7*x, 0) for x in np.ceil(p)])

fig=plt.figure()
ax=fig.add_subplot(111)

ax.scatter(p[m>=0], s[m>=0], marker = "o", color= colors[m>=0], s=100)
ax.scatter(p[m<0], s[m<0], marker = "s", color= colors[m<0], s=100)

ax.set_xlabel("polarity")
ax.set_ylabel("subjectivity")

plt.show()

2 Comments

It would be helpful if you were not just asking one question after the other but tried to come up with a solution based on what you already know. In this case the answer to how you select indizes based on multiple conditions has actually already been given in Diziet's answer.
Really appreciate the comment. Problem is I am very new to matplotlib so experimenting on the input given by others by trial and error. I will try for the solution. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.