D3.js log scale stacked bar - d3.js

I am trying to construct a single column stacked bar chart with d3.js, my data array might look like this in some instances:
[{x: 0, y:1, y0: 0}, {x: 1, y: 10, y0: 1}, {x: 2, y:2, y0: 11}]
but in another instance it might look like this:
[{x: 0, y:20, y0: 0}, {x: 1, y: 300, y0: 20}, {x: 2, y:2189, y0: 320}]
I am having trouble with the log scale. The chart's height is always 150px and the rectangles for the stacked chart should not be smaller than 10px ever.

Related

pyqtgrapth cololbar matching to data valuse

I am a new pyqt user and not quit familar with it.
I have a dataset min=-50000, max=100000. I need to plot it using pyqtgrapth. However the color bar I am using dose not match with the actual values of the data.
I have to add, data normalized to one before ploting.
Data = Data/np.max(Data)
Here are my codes to plot them:
entstops = [0, 0.005, 0.01, 0.025, 0.05,0.1, 0.2, 0.4, 0.5, 0.6, 0.7, 0.8,0.9,1.0]er code here
enself.colors = 255 * array(
[[1,1,1,0.7],[1, 1, 1, 0.2],[1, 1, 1, 1], [0, 0, 0, 0.3], [1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [1, 1, 0.5, 1], [1, 0.89, 0.03, 1],
[1, 1, 0, 1], [1, 0.165, 0, 1],[1, 0, 0, 1],[0.128,0,0,1], [1, 1, 1, 1]])
entercolormap = pg.ColorMap(stops, self.colors)
lut = colormap.getLookupTable(-0.1, 1.0, 512)
self.frog_preview_draw["data"] = pg.ImageItem()
self.frog_preview_draw["data"].setLookupTable(lut)
What I am expecting is, having more colors around (-0.1-0,1) and less colors for bigger numeber, because I would like to see the noise of my data.
Thank you

How do I histogram a color image using Numpy? [duplicate]

I use python opencv load an image which has shape (30, 100, 3), now want to count the frequency for all the colors, by color, I don't mean individual channel, I mean channel combination. Meaning 3 channel list, e.g. [255, 0, 0] for red, [255, 255, 0] for yellow, [100, 100, 100] for another color. So I want the last axis(channel) to be treated as a whole and count its frequency.
Is there any built-in function in opencv or numpy which can easily treat the 3 channel list as one element and count its frequency?
You could use np.unique with its new axis argument functionality that does grouping -
np.c_[np.unique(im.reshape(-1,3), axis=0, return_counts=1)]
Sample run -
In [56]: im
Out[56]:
array([[[255, 255, 255],
[255, 0, 0]],
[[255, 0, 255],
[255, 255, 255]]])
In [57]: np.c_[np.unique(im.reshape(-1,3), axis=0, return_counts=1)]
Out[57]:
array([[255, 0, 0, 1],
[255, 0, 255, 1],
[255, 255, 255, 2]])

D3 Interpolate Line Chart on Time Axes

I have a pretty basic line chart I'm using to plot some data on based on time. This is all working fine however the problem I need to address is when the plots along the time axes are very close.
Consider the following data points:
{ "Id": 101, x:15, y: 10, "OriginDt": "2012-12-01T18:49:06"}
{ "Id": 101, x:15, y: 10, "OriginDt": "2012-12-02T28:49:06"}
{ "Id": 101, x:15, y: 10, "OriginDt": "2012-12-03T18:49:06"}
{ "Id": 101, x:15, y: 10, "OriginDt": "2012-12-04T08:10:06"}
{ "Id": 101, x:15, y: 10, "OriginDt": "2012-12-04T09:21:06"}
{ "Id": 101, x:15, y: 10, "OriginDt": "2012-12-04T11:32:06"}
For the first 3 points the chart looks fine but as soon as it gets to data that occurs within a short space of time the data starts to get bunched up and very hard to appreciate in the chart.
I've been trying to find a sample online that deals with something like this but haven't found it yet.
I've tried using the basic interpolation commands on the axes but these don't work.
How can I change my axes:
var yAxis = d3.svg.axis()
.tickSize(-width).tickPadding(10)
.scale(y)
.orient("left");
To "spread out" the points in these situations?
This would be a comment but I don't have the necessary reputation.
You might want to look into cartesian fisheye distortion

Algorithm to combine similar hashes in an array

The question is mostly in the title:
given an array
array = [{x: 1, y: "jacksonville"},
{x: 2, y: "atlanta"},
{x: 1, y: "tampa"},
{x: 2, y: "atlanta"},
{x: 2, y: "jacksonville"},
{x: 2, y: "miami"}, ]
Whats a good method to attain the following result
array = [{x: 3, y: "jacksonville",
{x: 4, y: "atlanta"},
{x: 1, y: "tampa"},
{x: 2 ,y: "miami"}]
The input are a simplistic version of what i'm really working with but as you can see i'm attempting to remove duplicates of the y values but retain the x values, I know i can run a merge on each hash combining two hashes by passing a black to Hash#merge but i'm having difficulty comparing all hashes to each other or finding the duplicates. Looking for a performant solution.
Here:
array.group_by{|e|e[:y]}.map{|k,v|{x:v.reduce(0){|a,b|a+b[:x]},y:k}}
First of all, you must have to create a convenient way to index y data. I would use Hash for that:
h = array.inject({}) do |ret, item|
ret[item[:y]] ||= 0 # initialize each item
ret[item[:y]] += item[:x] # increment count
ret # return the hash
end
With this Hash on hand, you can generate the new array:
h.map {|key, value| {x: value, y: key}}

How do I get rid of gray boundaries for ArrayPlot in Mathematica?

I have the following plot.
lst={{1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0,
0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0,
0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0,
1}, {1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1}};
ArrayPlot[lst, Mesh -> All,
MeshStyle -> Directive[AbsoluteThickness[3.], Gray, Opacity[0.1]]]
But it does not look as I expected, in which I want the grey boundaries/grids for the black squares to be overshadowed by the color of these black squares. Only show the gray boundaries/grids of the white squares.
This isn't something that can be easily solved using built in options (AFAIK). You can define a custom function that plots the gridlines only at those rows and columns that you need and masks the others. Here is my solution:
gridArrayPlot[mat_?MatrixQ, options___] := Module[{dim = Dimensions#mat},
Show[
ArrayPlot[mat, Mesh -> ({Range[#1 - 1], Range[#2 - 1]} & ## dim), options],
ArrayPlot[mat, Mesh -> ({{0, #1}, {0, #2}} & ## dim),
ColorRules -> {0 -> Directive[Opacity#0, White]},
options /. Opacity[_] :> Opacity[1] /. (RGBColor[___] | GrayLevel[_]) :> White
]
]
]
The above solution first plots an ArrayPlot, drawing the mesh everywhere except at the outer boundaries and overlays a second ArrayPlot with the White cells set to be transparent, and draws White mesh lines on the outer boundary (to mask the bits sticking out from the previous plot).
You can call the above function as
gridArrayPlot[lst,MeshStyle -> Directive[AbsoluteThickness[3.],Gray,Opacity[0.1]]]
and the output is:
How about just ditching grid lines and mesh lines and using Epilog and Line?
ArrayPlot[lst, Mesh -> False, Frame -> False,
Epilog -> {GrayLevel[0.5], AbsoluteThickness[1],
Line#Table[{{2 + i, 8}, {2 + i, 1}}, {i, 0, 5}],
Line#Table[{{1, 2 + i}, {8, 2 + i}}, {i, 0, 5}]}]
This is obviously specific to this list of data but is straight forward to generalize to data where you have "x" unit black "perimeter" and "y" times "y" white square (i.e. a y+2x list of rows and columns).
gridArrayPlot[mat_?MatrixQ] := Module[{dim = First#Dimensions#mat,
white = Length#Cases[mat, {__, 0 .., __}], black, left, right, grid},
black = (dim - white)/2;
left = black + 1;
right = dim - black;
grid = white - 2;
ArrayPlot[mat, Mesh -> False, Frame -> False,
Epilog -> {GrayLevel[0.5], AbsoluteThickness[1],
Line#Table[{{left + i, right}, {left + i, black}}, {i, 0, grid}],
Line#Table[{{black, left + i}, {right, left + i}}, {i, 0, grid}]}]
]

Resources