With nvd3.js/d3.js, how can you have a scatter or line chart with discrete/non-numeric/non-time-series X-Axis values? - d3.js

For example, I want a scatter chart that looks like:
100| X o
| o
60|
| o X X
40|
|
20| o X
|
0|______________________________________________________________
Apples Carrots Dragons Monkies
Where X and o are different sets of data

The key is to use an ordinal sale for the x axis. Ordinal scales are constructed by calling d3.scale.ordinal(). The domain would be an array of your titles (you can use the native map method on your array of data to pull out the titles dynamically). The range needs to by an array of the same length as the domain array, specifying each xcoordinate in pixels that will correspond to the titles.
See this example here: http://tributary.io/inlet/5788637
You can fork it and try adding a yScale (in this case it will be constructed by d3.scale.linear since it is a continous number scale) and making a y axis following a similar pattern as the x axis.

Related

How I can plot colored scale in plot in R studio without ggplot package?

Hi, Any idea how i could create plot like the attached one.it is scatter plot for xdata and y data and the color is based on values of the third column.I would like not to use ggplot only basic R and I am not able to produce the scale thanks. I tried to read the data frame and drfine the y and x and make the scatterplot, color the points df<-AllData
y<- AllData $column1
x<- AllData $column2
LINEAR REGRESSION
reg<-lm(y ~ x, data = AllData )
plot(x,y, type="p", pch=20,ylab="",xlab="",axes=F,ylim = c(0,80),xlim=c(0,80),cex=1,col=ifelse(AllData$column3<10, 'blueviolet',ifelse(AllData$column3<20, 'blue4',ifelse(AllData$column3<30, 'blue2',ifelse(AllData$column3<100, 'darkgoldenrod', 'red'...etc till 100))))

How to use hash table to determine if there are two points in an array have slope k

How to use hash table to find specific slope for points in an array to improve time complexity?
The trick is simply to index the points by the y values with the slope expectation effectively removed: i.e. "standardised y" y' = y - x * slope
Put another way, if you draw a line at slope k from two or more points back to the same point on the y axis (where x = 0), they'll form a solution set.
Note that you may get actual or approximate (due to rounding) matches on y' values, so should:
use a hash table that supports duplicate keys (e.g. C++'s unordered_multimap) or - clumsier - map from y' to a container of x,y points, and
use whatever heuristic you deem best for extremely close y' values should be treated as equal (or otherwise, do some check/filtering on the raw x,y coordinates when y's are close/equal)
Pseudo-code:
for x,y in points
y' = y - x * slope
hash_multimap.insert(key=y', value=(x, y))
traverse hash_multimap
solution set includes any same-y'-key entries AND
values so close they're presumed rounding errors
e.g. one/two/a-few applications of
std::next_after(y', MAX_DOUBLE) to find nearest
representable double values (separate O(1) lookups);
values can be erased or marked invalid once outputted

d3 floating grouped bar with ranged values in a timeline

im trying to understand what tools i need to use as im new to d3 and didnt find any thing related...
i need a area chart that is like bars but can float and be on multiple values both on the x and y axis.
in this example the values are days but it might be hours/months etc...
need to know the direction i need to go.. / the right term to search...
There's no significant difference between drawing this chart and a normal bar chart.
And you need to define some scales that will map the values in your data to co-ordinates on your chart.
You need to draw some rect shapes.
So, in the above example you would define a time scale that, given an input date, will map that to a certain x co-ordinate on your chart. You can then use that to determine both the x co-ordinate for where the left-hand-side of a rectangle will be, and to work out how wide the rectangle needs to be.
const xScale = d3.scaleTime()
.domain([d3.min(dateValuesInMyDataset, d => d.date), d3.max(dateValuesInMyDataset, d => d.date)])
.range([0, widthOfMyChart]);
The above xScale if given the earliest date in your dataset would return the value 0, because this is the x co-ordinate representing that date.
Similarly, you would want to construct a linear scale which defines how to map the numerical range of values in your dataset, to the y co-ordinates in your chart. Then you can use the scale to determine the y value and height of all of the rectangles in your chart.
There are lots of good examples of this on ObservableHQ.com that you can browse and see the code for.

Error while converting a 3d matrix into an animated gif in Matlab

I am attempting to make a movie from a 3d matrix, which is made multiple 2d matrices and the third dimension is time.
I have read the following question witch is pretty much the same and I have attempted to do the same.
How to make a video from a 3d matrix in matlab
The 3d matrix I want to play is stored in a object instanced A.
a.movie; % 3D matrix
X = permute(a.movie,[1 2 4 3]); % 4D matrix
movie = immovie(X,map); % map is the colormap you want to use
implay(movie);
I would like to know why should a.movie be permuted? And what is the map referred?
How can I define 0 as blue and 100 as red?
The post you linked us to exactly answers that. immovie expects a m x n x 1 x k matrix where m and n are the rows and columns of 1 slice from your 3D matrix, and k is the number of slices. You currently have your 3D matrix set up to be m x n x k. Therefore, by permuting, you are artificially creating a 4D matrix from your 3D original matrix. Simply put, you can think of your 3D matrix as having a singleton 4D dimension: m x n x k x 1. The job of permute here is to swap the 3rd and 4th dimension - that's why you see the [1 2 4 3] vector in the permute call. The first and second dimensions represent the rows and columns, and you leave those empty.
Now that answers the permute question. The map is defined as a colour map. This maps each value in your 3D matrix to a unique colour. Basically, the colour map is a M x 3 matrix where row in this matrix corresponds to a unique colour. Each column represents a colour channel. Therefore, the first column represents the proportion of red you want, the second channel is the proportion of green and the last is the proportion of blue. Keep in mind that these colours should be normalized between [0,1].
The goal of the colour map is to take each value in your 3D matrix, and figure out which colour that value maps to. The way to do this is to use each value in your 3D matrix exactly as it is and use this to access the row of the colour map. This row gives you the colours you want. Now, I'm assuming that your values in the 3D matrix span from 0 to 100.
If you want the colours to span between blue and red. The blue colour has an exact colour of RGB = (0,0,1) assuming normalized coordinates and similarly, the red represents the exact colour of RGB = (1,0,0). Therefore, start off with RGB = (0,0,1), then start linearly increasing the red component while linearly decreasing the blue component until the red is 1 and the blue is 0.
What we can do is figure out how many unique values there are in your matrix, then we can create our colour map that way so we can ensure that each value in your matrix gets assigned to one colour. However, this will require that a.movie be redefined to ensure that we can assign a value to a colour.
Therefore, I'd create your colour map like this:
[unq,~,id] = unique(a.movie);
movie_IDs = reshape(id, size(a.movie));
M = numel(unq);
map = [linspace(1,0,M).', zeros(M,1), linspace(0,1,M).'];
Now, go ahead and use map with the above code to create your movie.
X = permute(movie_IDs,[1 2 4 3]); % 4D matrix
movie = immovie(X,map); % map is the colormap you want to use
implay(movie);
However, the colour map you're looking at is the jet colour map. Therefore, you can simply just create a jet colour map:
map = jet(M);
However, you must make sure you run through each value in a.movie and assign a unique integer to each value to ensure that there are no gaps in your data and every value gets assigned to a new value that goes up from 1 to M in order for the movie to properly access the right colour.
MATLAB has a bunch of built-in colour maps for you to use if you don't feel like designing your own colour map. http://www.mathworks.com/help/matlab/ref/colormap.html#inputarg_map - However, from what I see in your post, making the colour map is what you want to do.

How to make half a photo negative

I have my code to negative out a photo but I am struggling trying to figure out how to negate only half either the left side or the rigth side of the image. Below is the code for negative that I know I can't figure out how to get half the pixels I need.
def negative(picture):
for px in getPixels(picture):
red=getRed(px)
green=getGreen(px)
blue=getBlue(px)
negColor=makeColor(255-red,255-green,255-blue)
setColor(px, negColor)
First off you need to find the width of the picture and divide that by 2
halfWidth = getWidth(picture) / 2
Because you don't want to iterate through all the pixels you can't use for px in getPixels(picture):. What you will need is the range() function in conjunction with 2 for loops such as
# Iterate along the Y axis
for y in range(0, getHeight(picture)):
# Iterate along the X axis but only for half the width
for x in range(0, halfWidth):
# Get the pixel at X, Y coordinate from picture
px = getPixel(picture, x ,y)
The rest of your code should work with this.

Resources