networkx: set different alpha for each node? - python-3.x

I want to set a different alpha for each node. It's easy to set a different color for each node but alpha only excepts a single value. Anyone know how to do this?
(My goal is to have a color gradient from blue to red. If the value is below the midpoint it's blue and if above it's red. Alpha would set the gradient.)
nx.draw_networkx_nodes(g,pos=pos,nodelist=nodelist,
node_color=node_color, alpha=1.0)

The alpha value will control transparency, not color.
If you are already supplying the node_color parameter, your nodes will be colored.
If you want them to go from blue to red based on those intensities, instead of alpha, use a colormap. E.g. seismic colormap goes from red to blue.
nx.draw_networkx_nodes(g, pos=pos, nodelist=nodelist, node_color=node_color, cmp='seismic')
EDIT:
I stumbled across some odd behavior trying to dig deeper into this.
I would have thought that networkx's plotting would behave like matplotlib's scatter plot, and accept rgba colors, allowing you to specify particular colors and transparency. But when I call draw_networkx_nodes with an array of 4-tuples to denote rgba colors, it sets all alpha values to 1.
rgba_colors = np.zeros((5,4))
rgba_colors[:,0] = np.linspace(1,0,5) #red
rgba_colors[:,2] = np.linspace(0,1,5) #blue
rgba_colors[:,3] = np.linspace(0.1,1,5) #alpha values
plt.scatter([1,2,3,4,5],[1,2,3,4,5], color=rgba_colors)
This plots some points with varying color and transparency. But, if you try plotting with networkx, not only does the resulting plot have no transparency, the variable rgba_colors has actually been modified.
G = nx.complete_graph(5)
pos = {0: [0,0], 1: [1,1], 2: [2,2], 3: [2,1], 4: [2,3]}
nx.draw_networkx_nodes(G, pos=pos, node_color=rgba_colors)
print(rgba_colors)
I'm not sure why this is happening.

Related

What's the point for the GameBoy of having multiple color palettes?

Each pixel is composed by two bits, allowing up to 4 shades of gray. From the LCD Monochrome Palettes section of the pandocs we can develop the algorithm for getting the color (in the case I've understood it correctly):
COLOR_NUMBER_PALETTE_BITS = {
0: (1, 0),
1: (3, 2),
2: (5, 4),
3: (7, 6)
}
COLORS = {0: WHITE, 1: LIGHT_GRAY, 2: DARK_GRAY, 3: BLACK}
def get_pixel_color(palette_address, color_number):
palette = read_memory(palette_address)
high_bit, low_bit = COLOR_NUMBER_PALETTE_BITS[color_number]
color_high_bit = get_bit(palette, high_bit)
color_low_bit = get_bit(palette, low_bit)
color = (color_high_bit << 1) | color_low_bit
return COLORS[color]
But just looking at the function signature we can deduce that same color numbers may result in different colors; it depends on the palette we're using.
My question is, why do we need multiple color palettes when two of them are identical, and the only difference with the third one is that 0 is transparent instead of white? Why do the palette definition change, instead of the color number used to get the color from the palette?
What do you mean "two of them are identical"? You have three different color palettes. One for background and window. Two for sprites which you can select for each sprite individually using special bit in sprite attributes. Why changing palette instead of color numbers? It's much cheaper to change color palette or sprite attributes than to rewrite each tile of a sprite or window to achieve some effect on the screen. You can even change palettes in realtime as the Gameboy draws the image on the screen.
For example, let's say I wanted to implement flashing effect where whole background flashes between normal colors and white or black. Instead of rewriting every background tile on every frame I could just change the palette and time it exactly to only affect parts of the screen I need. There's even an article describing similar technique.

plotting multiple graphs on same axis with same color but different color intensities

I am attempting to plot multiple graphs on the same axis with the same color but different color intensities.
However, my results appear to have the same intensity for all colors, not sure how to fix this.
import numpy as np
import matplotlib.pyplot as plt
iterations = 3
for i in range(iterations):
data = np.random.normal(size=10)
plt.plot(data, c="red",alpha=1/3)
plt.show()
all 3 plots are having the same color intensity
Just remove c="red" from the code it will work fine
plt.plot(data,alpha=1/3)
Well, you specified "red" for all rows. Your computer doesn't know that you expect a different red each time.
As you already tried, you can adjust the alpha value; however you need to set a different value at each iteration. You can do that by using the i loop variable.
For finer color control, use RGB "tuple" instead of a color name. That is, a tuple of three floats between 0.0 and 1.0, specifying the "red", "green" and "blue" component.
The standard "red" is (1.0, 0.0, 0.0).
To get lighter shades, add some green and blue by increasing the 0.0 's. To get darker shades, decrease the 1.0.
Code::
for i in range(iterations):
data = ...
plt.plot(data, c=(1.0, 0.1*i, 0.1*i), alpha=1/3)
# or
plt.plot(data, c="red", alpha=1.0-0.1*i)
See also https://matplotlib.org/users/colors.html

Matlab colormap reproducing gnuplot pm3d 30,31,32 RGB colour space

I am trying to implement a colorbar in Matlab that looks linear when printed in grey. The most straight forward approach I though would be to implement the gnuplot pm3d 30,31,32 RGB colour space. On this website I found a good introduction, which works OK for some of the easier gnuplot schemes. However, when trying to implement the 30,31,32 scheme I run into trouble.
The gnuplot instructions are
30: x/0.32-0.78125 31: 2*x-0.84 32: 4x;1;-2x+1.84;x/0.08-11.5
And I interpreted this such that for the blue channel I have to apply four different equations. One for each quarter of intensity values. This is what I have tried so far
x = linspace(0,1,128);
r = x/0.32-0.78125;
g = 2*x-0.84;
b(1:length(x)/4) = 4*x(1:length(x)/4);
b(length(x)/4:length(x)/2) = 1;
b(length(x)/2:length(x)*.75) = -2*x(length(x)/2:length(x)*.75)+1.84;
b(length(x)*.75:end) = x(length(x)*.75:end)/0.08-11.5;
pm3d303132=[r;g;b]';
but unfortunately it doesn't work. I end up with negative values and values that exceed 1, which Matlab cannot interpret.
I did read in the show palette rgbformulae help that
* negative numbers mean inverted=negative colour component
But I don't know how to implement this in Matlab; and I also don't understand how to deal with values exceeding 1. Can anyone help?
Ignoring the out of bound values seems to work. (http://juluribk.com/2011/05/18/843/)
pm3d303132(pm3d303132<0)=0;
pm3d303132(pm3d303132>1)=1;

Built in colormaps in Matlab

I want a lighter version of the "cyan" color, using the function colormap('cyan'). How do you do this?
Check out the function BRIGHTEN:
X = spiral(8);
image(X)
colormap(winter), colorbar
brighten(0.6)
Another trick is to right click on the colorbar and select Interactive Colormap Shift, this allows to shift the color-to-data mapping using mouse dragging.
Pure cyan is represented by the RGB triple [0 1 1]. To make it lighter, just increase the red component (ex: [0.5 1 1]), thus moving it closer to pure white ([1 1 1]). If you want to make a colormap that spans from pure cyan through lighter shades of cyan all the way to pure white, you can do the following:
nValues = 128; %# The number of unique values in the colormap
map = [linspace(0,1,nValues)' ones(nValues,2)]; %'# 128-by-3 colormap
Now you can set the colormap to the one made above using the COLORMAP function:
colormap(map);
For more discussion of colors in MATLAB, check out this link.
For me colormap('cyan') fails because cyan is undefined.
However, you can create your own colors easily. If cyan is equivalent to [0,1,1] a lighter color would be [0,1,1] + [.1,0,0] = [.1,1,1] or rather just increase the R in RGB to increase the luminosity.

How do I set a surf to one color (no gradient) in my matlab-plot?

My dataset consists of three vectors (x,y and z). I plot these values as dots in a 3d-plot with plot3(x,y,z), which is fine. I also want to show a plane in the same plot. To get the data of this plot I use linear regression on x and y to get a new z.
This is how it looks:
(source: bildr.no)
I want the surf to be filled with only one color (say light blue or gray) and set the opacity, to make it see-through. How can I do this?
The easiest way to create a surface that has just 1 color and a given transparency value is to set the 'FaceColor' and 'FaceAlpha' properties of the surface object:
hSurface = surf(...your arguments to create the surface object...);
set(hSurface,'FaceColor',[1 0 0],'FaceAlpha',0.5);
This example sets the surface color to be red and the transparency to 0.5. You can also set the edge properties too (with 'EdgeColor' and 'EdgeAlpha').
It is not clear to me what you want to do. When you say one color for the surf, do you mean exactly one color, or do you mean you want shades of gray?
Here is some code that will do a variety of things, you can choose which lines to use:
x = rand(1,20);
y = rand(1,20);
z = rand(1,20);
[X,Y] = meshgrid(linspace(0,1,10),linspace(0,1,10));
Z = rand(10)*0.1;
clf
plot3(x,y,z,'.');
hold on
h = surf(X,Y,Z)
hold off
%% This will change the color
colormap(copper)
%% This will remove colordata
set(h, 'cdata',zeros(10))
%% This will make transparent
alpha(0.5)
Completing the answer from gnovice, an extra ingredient in set(hsurface...) may be required (Matlab R2010b 64):
hSurface = surf(...your arguments to create the surface object...);
set(hSurface, 'FaceColor',[1 0 0], 'FaceAlpha',0.5, 'EdgeAlpha', 0);
to make invisible the point-to-point edges of the plotted surface
#matlabDoug has what you need, I think. The property cdata holds color data that gets a color map applied to it. Setting it to an array the same size as your surface data, with each element in that array having the same value, will make your surface one color. With the default color map, setting everything in cdata to zero will make your surface blue, and setting everything to 1 will make the surface red. Then you can play with the alpha to make it transparent.

Resources