How to assign a colour to a variable in Fore (colorama) - python-3.x

I am trying to make a function that gets an inputted string and returns it with a random colour. (The random colour is defined earlier)
print(Fore.randomColour + inputVariable)
Whenever I try something along those lines, I end up with the error:
AttributeError: 'AnsiFore' object has no attribute 'randomColour'
I have tried using getattr, but I still couldn't get it to work. Any help will be much appreciated. Thanks.

You can make a list with your defined colors and then pick it randomly.
import random
bcolors = ['\033[95m','\033[94m','\033[92m','\033[93m','\033[91m','\033[1m']
print (random.choice(bcolors) + "random color" + '\033[0m')
where '\033[0m' means reset

Related

Visual Python using a for loop to track a changing axis?

I have a giant array that I am importing from a text file holding 200,000 values which all describe how an arrow should be traversing along the x axis. Mx is the name of the array that I have imported from this text file. I am using visual python to try and simulate this movement across the axes.
arrow = arrow(length = 1.0,pos = (0,0,0),axis=(0,0,0),color=color.blue)
for i in range(len(Mx)):
rate(60)
arrow.axis.x = Mx[i]
When I run my code the arrow remains stationary, and "QObject::killTimers: timers cannot be stopped from another thread" this error message pops up. I am not sure why. Any advice would be really helpful.
What I found is that this fixed it for some reason.
arrow = arrow(length = 1.0,pos = (0,0,0),axis=(0,0,0),color=color.blue)
for i in range(len(Mx)):
rate(60)
x = Mx[i]
arrow.axis.x = x
Not 100 percent sure why, but I am guessing it has something to do with how python handles the array that I imported from the text file.

Formatted String from List Psychopy

My task is a variation of a multiple object tracking task. There are 7 circles on the screen. It randomly selects 3 circles to change the color (red, green, blue) briefly to indicate to the participant to track these circles. After the color change, all the circles will change to the same color and the circles will move for a period of time. When the circles stop moving, a response prompt will appear, where the participant is to select one of the three colored circles ('select the red/green/blue circle'). I am having difficulty inserting which color circle to select into the formatted string. I keep getting the error message: unsupported operand type(s) for %: 'TextStim' and 'list'
I'm not sure if I need to or how to convert these lists, so any help would be much appreciated!
n_targets = 7 #seven locations
circles = [] #setting up the circle stimuli
for i in range(n_targets):
tmp = visual.Circle(win,radius = 27,units = 'pix',edges = 32,fillColor='white',lineColor = 'black',lineWidth = 1, pos=(posx[i],posy[i]))
circles.append(tmp)
cols = ['blue','red','green'] #3 colors the circles will change to
targets = random.sample(circles,3) #randomly select 3 of the 7 circles
TrialTarget = random.sample(targets, 1) #select 1 of the 3 circles to be the target for the trial
#code for movement would go here (skipping since it is not relevant)
#at end of trial, response prompt appears and ask user to select target and is where error occurs
ResponsePrompt = visual.TextStim(win, text = "Select the %s circle") %TrialTarget
In this line, you are trying to create a formatted string from a TextStim object and a Circle stimulus object rather than a string object and another string object:
ResponsePrompt = visual.TextStim(win, text = "Select the %s circle") %TrialTarget
i.e. ResponsePrompt is clearly a visual.TextStim, as you are creating it as one, and I think TrialTarget is a visual.Circle stimulus, as you randomly sample it from a list of Circles.
I'm guessing that you actually want to incorporate the colour label into the prompt text. So to fix both problems (the type incompatibility and the formatting syntax), you need to actually get one of the elements of cols, called say trialColour, and use something like this:
ResponsePrompt = visual.TextStim(win, text = "Select the %s circle" % trialColour)
i.e. here trialColour is actually a string, and the formatting operation is brought inside the brackets so it applies directly to the text string "Select the %s circle"
That should hopefully fix your immediate problem. You might also want to investigate using random.shuffle() to shuffle lists in place instead of random.sample().

Matlab - Using symbols in gscatter

Ok, so I'm trying to use gscatter to plot 8 different points in a figure. These 8 points are all different and thus I want to give them different symbols. I know that gscatter will automatically assign them different colors, but I also want to be able to use the figure in black and white. I have written the following code:
lincol = {'k';'k';'k';'k';'k';'k';'k';'k'};
linsym = {'+';'o';'*';'.';'x';'s';'d';'^'};
limits = [-1 1 -1 1];
close all
for i = 1:3;
figure(i); hold on
gscatter(RfootXdistpertRel(:,i),RfootYdistpertRel(:,i),lincol,linsym);
legend('Pert1', 'Pert2', 'Pert3', 'Pert4', 'Pert5', 'Pert6', 'Pert7', 'Pert8')
hline(0);
vline(0);
axis(limits);
end
According to the matlab syntax, I should be able to specify color and marker symbol in this way (gscatter(x,y,col,sym)). The variables used are 8 by 1 vectors, just as the lincol and linsym. However, it gives me an error:
Error using plot
Color value must be a 3 or 4 element vector
Can anyone help? It's just such a silly problem to have.
Luc
It seems that you have some errors in your code. The syntax for gscatter should include at least 3 parameters : x, y and group. It seems that group is missing.
Furthermore the definition of color and sym may be wrong. Try col = 'kkkkkkkk'; instead of lincol = {'k';'k';'k';'k';'k';'k';'k';'k'};.
I hope this helps.
Regards.
Jonay

How do I set custom colours on a pyqt QColorDialog?

I am bringing up a QcolorDialog like so:-
colour = QtGui.QColorDialog.getColor()
What I want to know is how to set the colours of the custom color swatches before I bring up the dialog. I have searched a lot and found the method setCustomColor() but I just can't get it to work. It repeatedly tells me
TypeError: argument 2 of QColorDialog.setCustomColor() has an invalid type
I have tried all manner of variations of how to create a QColor, but it never seems happy with it.This is what I am currently trying:-
mycolor = QtGui.QColor(0,0,0,0)
colour = QtGui.QColorDialog.setCustomColor(0,mycolor)
But it still gives me the same 'invalid type' error...
Any ideas?
All you need to do is:
colour_dia = QtGui.QColorDialog()
mycolour = QtGui.QColor(0, 0, 0, 0).rgba()
#This needs a integer value for colour
colour_dia.setCustomColor(0, mycolour)
selected_colour = colour_dia.getColor()

how to use an atomic vector as a string for a graph title in R

I'm trying to plot a graph from a matrix of z-scores in R, i would like to build a function to iterate through each column using the column header as part of the title and saving each graph as a png.I think I know how to do the iteration and saving graphs as pngs but I am getting stuck with using the vector as a string. I tried to upload the matrix with no column headers and then store matrix[1,] as a variable 'headers' to use. Then I tried to plot:
plot(1:30, rnorm(30), ylim=c(-10,10), yaxs="i", xlab = "Region", ylab = "Z-Score",main = "CNV plot of " + headers[i], type = "n")
I get:
Warning message:
In Ops.factor(left, right) : + not meaningful for factors
I try without the '+' and it says:
Error: unexpected symbol in ...
So then I looked around and found 'paste(headers[i],collapse=" ") which I though I could substitute in but it just puts the number '28' as the title.
I've tried what I thought was another potential solution:
plot(1:30, rnorm(30), ylim=c(-10,10), yaxs="i", xlab = "Region", ylab = "Z-Score",main = "Z-scores of " $headers[i], type = "n")
and I get:
Error in "Z-scores of "$headers :
$ operator is invalid for atomic vectors
I'm new to R and this seems like something that would be so simple if I happened to stumble across the right guide/tutorial after hours of google searching but I really don't have that kind of time on my hands. Any suggestions, pointers or solutions would be great??
If you want to insert values from variables into strings for a plot title, bquote is the way to go:
headers <- c(28, 14, 7) # an examle
i <- 1
plot(1:30, rnorm(30), ylim=c(-10,10), yaxs="i",
xlab = "Region", ylab = "Z-Score", type = "n",
main = bquote("CNV plot of" ~ .(headers[i])) )
Have a look at the help page of ?bquote for further information.
paste("CNV plot of", headers[i]), should work. You only need collapse if you are pasting vectors of length greater than one (headers[i] should be one length, even if header is not). R doesn't have any concatenation operators, unlike PHP, JS, etc (so +, &, . will not work, you have to use paste).
Note that your paste was paste(headers[i],collapse=" "), and if that just plotted 28, it suggests your headers vector doesn't contain what you think it does (if you didn't want 28 to be displayed, that is.
Try just looping through your vector and printing the paste command to screen to see what it displays (and also, just print the vector).

Resources