Get rid of the spaces histogram matplotlib [duplicate] - python-3.x

I'm making a bar chart in Matplotlib with a call like this:
xs.bar(bar_lefts, bar_heights, facecolor='black', edgecolor='black')
I get a barchart that looks like this:
What I'd like is one with no white gap between consecutive bars, e.g. more like this:
Is there a way to achieve this in Matplotlib using the bar() function?

Add width=1.0 as a keyword argument to bar(). E.g.
xs.bar(bar_lefts, bar_heights, width=1.0, facecolor='black', edgecolor='black').
This will fill the bars gaps vertically.

It has been 8 years since this question was asked, and the matplotlib API now has built-in ways to produce filled, gapless bars: pyplot.step() and pyplot.stairs() with the argument fill=True.
See the docs for a fuller comparison, but the primary difference is that step() defines the step positions with N x and N y values just like plot() would, while stairs() defines the step positions with N heights and N+1 edges, like what hist() returns. It is a subtle difference, and I think both tools can create the same outputs.

Just set the width 1 over the number of bars, so:
width = 1 / len(bar_lefts)
xs.bar(bar_lefts, bar_heights, width=width, color='black')

You can set the width equal to the distance between two bars:
width = bar_lefts[-1] - bar_lefts[-2]
xs.bar(bar_lefts, bar_heights, width=width)

Related

networkx: set different alpha for each node?

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.

3D line multiple colors

I need help with Matlab.
I have an Excel sheet with three columns: X, Y and Z. I have used plot3 function to make one 3D curve.
But I need to vary it in colors.
What function/functions do I need to make X, Y and Z in different 3 colors(each column one color)?
Could you please send me link, where I can find out the way, or just write the function/functions needed for it?
Here is the code:
VCG=xlsread('VCGsheet.xls');
figure(1)
plot3(VCG(:,1),VCG(:,2),VCG(:,3));
grid on
I know that plot3 isn't suitable for it.
I'm not aware of a native command that draws line with varying color.
I will also assume that VCG vector is a vector of RGB values, so one color per row.
RGB = VCG(:,1:3);
If you want, you can replace it (VCG(:,1:3)) with any other vector of color derived from your data. Here is an example if you have a single value that can be calculated for each of your points, in the vector T for example, and that you want to show as a color.
map = jet(256);
RGB = map(round((T(:)-min(T(:)))/(max(T(:)) - min(T(:)))*255)+1,:);
For the plot, I propose two different ways:
you can make use of the scatter3 function
It will print points, but no lines connecting the points. The color vector is set with VCG(:,1:3)
colormap('jet')
scatter3(VCG(:,1), VCG(:,2), VCG(:,3), 70, RGB, 'filled');
You can make a direct use of line in a for loop.
It is a bit slower, but it is generally ok for graphs.
for i=2:size(VCG,1)
line(VCG(i-1:i,1), VCG(i-1:i,2), VCG(i-1:i,3), 'color', RGB(i-1,:));
end
If you want both, just use the hold function
I hope I understood what you needed !

d3.js : getting the bars width or X position right?

I have a weird issue in my bar graph realized using d3.js: the 1 px padding between each rectangle appears irregular. I gather either or both the width or x position are the culprit but i don't understand what i'm doing wrong: the width is a fraction of the svg area and the X position is obtained via a D3 scale.
I've put a demo here: http://jsfiddle.net/pixeline/j679N/4/
The code ( a scale) controling the x position:
var xScale = d3.time.scale().domain([minDate, maxDate]).rangeRound([padding, w - padding]);
The code controlling the width:
var barWidth = Math.floor((w/dataset.length))-barPadding;
Thank you for your insight.
It's irregular because you are rounding your output range (rangeRound). In some cases, the distance between two bars is 3 pixels and sometimes only 2. This is because the actual x position is a fractional value and ends up being rounded one way in some cases and the other way on other cases.
You can mitigate the effect but changing rangeRound to range, but that won't eliminate it entirely as you'll still get fractional pixel values for positions. The best thing to do is probably to simply increase the padding so that the differences aren't as obvious.

Flot: How to keep x and y axes the same scale?

I want to plot on a standard Cartesian plane, so 1 unit on x axis has the same length as 1 unit on y axis. I also use the navigate plugin to zoom and pan. Is there anyway I can have this constraint? can't find anything about this in Flot api.
Thanks,
I had to do something similar and this was the first test: fiddle
The main point is that you set the max values for the axis in the same ratio as the width and height of your placeholder div:
options.xaxes[0].max = options.yaxes[0].max * $('#ph').width() / $('#ph').height();
$.plot($('#ph'), data, options);

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