How can I set the color for subsequent plot commands? - matplotlib

I have to issue many plot commands to create a picture. As in the following example:
color="C0-"
plt.plot([1,5,4], color)
plt.plot([3,7,8], color)
To simplify code I would prefer something like:
plt.set_color("C0-") # ERROR
plt.plot([1,5,4])
plt.plot([3,7,8])
Is this possible?

If no color argument is given, the color of the plots is determined by the color cycle in use. You may change the color cycle at runtime to only have one single color, which effectively makes all subsequent plots that same color.
plt.gca().set_prop_cycle('color', ["blue"])
To get the first color of the current color cycle you may use
c0 = plt.rcParams["axes.prop_cycle"].by_key()["color"][0]
plt.gca().set_prop_cycle('color', [c0])

Related

Change support and resistance line color in mplfinance

Currently, I'm using this code. How can I change the hlines to red if it's a resistance and blue if it's a support?
mplfinance.plot(df,
type = 'candlestick',
style = 'binance',
hlines=dict(hlines= support_resistance,linestyle='-', linewidths = (1,1)),
volume = True)
I'm getting results like this:
hlines=dict(hlines= support_resistance,linestyle='-',linewidths = (1,1),colors=('b','r')
See for example cell "In [6]" in this tutorial: https://github.com/matplotlib/mplfinance/blob/master/examples/using_lines.ipynb
You may need to include as many colors as you have support_resistance lines. So for example, maybe something like: colors=['b','r','b','r','r','r']
Regarding handling support resistance colors dynamically (per your comment) it is not appropriate for mplfinance to provide algorithms for determining support or resistance, only to provide tools to make it easier for you to visualize them.
Also, each user may have their own specific way of determining support or resistance.
Presumably as you are building the support_resistance list, at the point in your code where you are adding a specific price to that list, you probably know whether that price represents support or resistance. At the same point in your code you should add a color ('b' or 'r') to the colors list. That way you dynamic build two lists: support_resistance, and colors which end up being the same length.
Based on the suggestion from Daniel, I made a list of colors as follows and it worked. support_resistance variable here consists of both support and resistance levels.
colors = []
for lvl in support_resistance:
if lvl > df['Close'][-1]:
colors.append('r')
else:
colors.append('b')

Dodged Bar Plots in ggplot - Defining colour by X axis as opposed to fill

Having some issue with dodged bar charts in ggplot. Essentially the only way I can dodge them is by changing fill to another variable, but I want to colour the paired plots by what they are on the x axis.
Have included a dummy picture of what I am trying to achieve.
I'm not sure if I'm missing something obvious here.
Many thanks in advance!
One trick is to use another not so useful variable to allow the dodge to happen. In the example below, I use color to separate them, but giving all of them the same color:
X = data.frame(Year=factor(rep(c(2016,2018),3)),
Sales=runif(6),Brand=rep(c("Toyota","Skoda","Ford"),each=2))
ggplot(X,aes(x=Brand,y=Sales,fill=Brand)) +
geom_col(aes(col=Year),position="dodge",show.legend=FALSE) +
scale_color_manual(values=c("black","black")) +
geom_text(aes(label=Year,y=0.5*Sales),position=position_dodge2(1),angle=90)

change CAMetalLayer background color

My CAMetalLayer background color is black, even if i'm assigning new color as the backgroundColor property.
Am i missing something? Thanks!
Link to the original project :
https://github.com/audiokit/MetalParticles
This project takes a rather unconventional approach to clearing the drawable's texture each frame: it replaces the textures contents with an array of zeros that is the same size as the texture (width * height * 4). Subsequently, it encodes some compute work that actually draws the particles. This is almost certainly not the most efficient way to achieve this effect, but if you want to make the smallest change that could possibly work (as opposed to experimenting with making the code as efficient as possible), Just fill the blankBitmapRawData array with your desired clear color (near line 82 of ParticleLab.swift).
I have gone through your code and can not see a place where you are setting background Color.
The metal layer is added as a sublayer to it, so you have to set it explicitly.
Add this line at the end of your init method in ParticialLab class and see if it works.
self.backgroundColor = UIColor.redColor().CGColor
I found that self.isOpaque = false was needed on the layer.

axis orient / position with dimple.js

Is there a way to explicitly position/orient an axis using dimple.js? I know that the first x-axis is added to the bottom and the second is added to the top, so I can get the desired outcome like this:
var xAxis = myChart.addMeasureAxis("x", "dur");
xAxis.hidden = true;
xAxis = myChart.addMeasureAxis("x", "dur");
...but that seems a little hacked.
I don't think there's any way around that. Here's the relevant code : https://github.com/PMSI-AlignAlytics/dimple/blob/master/src/objects/chart/methods/draw.js#L123
Because firstX is local to the draw function there isn't a way you could override it within the context of the loop.
You could possibly duplicate the logic used to position the top axis : https://github.com/PMSI-AlignAlytics/dimple/blob/master/src/objects/chart/methods/draw.js#L226
And then only add one axis (which would be positioned at the bottom), then manually move your axis after the drawing, like :
chart.draw();
chart.axes[0].shapes.attr('transform', ...);
But you would need to do this after every draw or else they'll be repositioned. The hack you have seems easiest.
You could open an enhancement ticket for this though : https://github.com/PMSI-AlignAlytics/dimple/issues?q=is%3Aissue+label%3Aenhancement+is%3Aopen

The best way to change background color in image in Cocoa

Little description:
I get an image from my scanner using Capture Core Framework and background color is gray. So I want to do white background.
I'm looking for best way to remove this color. I looked at Image Core Filters. And as I understood I should use those ones, but I couldn't find included/ready filterfor this task. So, Do I have to write it myself?
It means to remove color I should write my own filter and apply it to my images? Right? Thank you.
UPD: I develop for Mac os
Why not just simply change the brightness of your image? If this gray background color value is fixed then you can increase the brightness a little bit, as a preprocess step, until you're satisfied.
A filter isn't quite the right tool for the job here. Filters apply a colour-shifting algorithm, pixel by pixel, to all of the pixels in an image.
This would be appropriate in your case if there was something completely unique about the pixels that you want to turn to white. For example, if the background you wish to eliminate was in a very narrow colour range that did not occur in any other part of the image. This is the technique used in greenscreen/bluescreen filming, which only works if that green or blue colour does not occur anywhere in the image regions you want to keep.
But - as you say in one of your comments - you cannot do this as that grey level you want to get rid of will not be unique to the background, so any filter you apply that would pick out those pixes may also affect pixels in the scanned subjectmatter.
What you really need is a way to select a region of interest and apply a filter to that region alone. You could use openCV for this. In fact it has a function that can achieve your result in one go:
http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=floodfill#int
floodFill(InputOutputArray image, //image to process
Point seedPoint, //starting pixel
Scalar newVal, //New value of the repainted domain pixels
Rect* rect, //optional output param (you won't need it)
Scalar loDiff, //max lower brightness/colour diff to select
Scalar upDiff, //max upper brightness/colour diff to select
int flags) //you want FLOODFILL_FIXED_RANGE
This function starts from a seedPoint, which should be any pixel that you can guarantee will be a part of the background grey you want to eliminate. (0,0) might work for you. It then interrogates neighbouring pixels, including them in the ROI array if they are sufficiently similar. The resulting array is a connected region. If your background grey uniformly falls between loDiff and upDiff - and your subject scan has a defined edge which does NOT fall into this range, you will get your result - selection and remapping of all background pixels to newVal (white).