1
$\begingroup$

I've been given some data in a particularly (in my opinion) odd way, namely an array of points connected to each other via lines, whereby each line has for a lack of a better word, "attribute" assigned to it. Being a numerical value between {1,4}.

In poorly hacked up code, I've managed to simply draw the lines connecting the points in a plane for visualizations sake.

Each blue line represents a value between 1 and 4, and the black lines are a simple grid. For clarity. The grid goes from [0,9] in y and [0,8] in x at 0.5 steps.

Graphics[{{Blue, Line[pts]}, lho, lver}, Frame -> True, 
PlotRange -> {{0, 8}, {0, 9}}]

enter image description here

I would like to build up an average of each 1x1 grid square of the values of each line inside said square. This way each square will become a single point value that I can now attribute to a {x,y,z} form with z being the average and x, y being coordinates in the grid.

For example, Square 1:1, starting at the bottom left has 8 lines within it, 4 of them having the value 2.45556, and the other 4 having the value 1.6690. Doing the math (4*2.45556+4*1.6690)/8 = 2.06228

Doing this for another square, such as 1:4 (starting from the left corner bottom corner again) would only see two lines two of which are shared from 1:1, so assuming those two lines both have the value 2.45556, it's average would be also 2.4556.

I can now have the points at {0.5 ,1.5 ,2.06228} and {3.5,1.5,2.45556} for further analysis. This particular example as a demonstration has 64 squares in total, however this will scale up to several hundred later.

First, is it possible to give a line a numerical value? If such a thing is possible, how would I attempt turning each square and its segment(s) of a line(s) into an average to be used later?

My first thought would be to give each line a specific colour from a function that scales {1,4} into a gradient from blue to red, then somehow using those values. But this is far beyond my skills in mathematica.

In the end I would like to turn this array of data of mine into a density plot similar to here.

3D heatmap density plot

pts, lve, and lho added updated lver which now has all square boundries

Thank you in advance.

pts = {{0, 1}, {8, 1}, {0, 1}, {8, 2}, {0, 1}, {8, 3}, {0, 1}, {8, 
2}, {0, 1}, {8, 4}, {0, 1}, {8, 5}, {0, 1}, {8, 6}, {0, 1}, {8, 
7}, {0, 1}, {8, 8}, {0, 2}, {8, 1}, {0, 2}, {8, 2}, {0, 2}, {8, 
3}, {0, 2}, {8, 2}, {0, 2}, {8, 4}, {0, 2}, {8, 5}, {0, 2}, {8, 
6}, {0, 2}, {8, 7}, {0, 2}, {8, 8}, {0, 3}, {8, 1}, {0, 3}, {8, 
2}, {0, 3}, {8, 3}, {0, 3}, {8, 2}, {0, 3}, {8, 4}, {0, 2}, {8, 
5}, {0, 3}, {8, 6}, {0, 3}, {8, 7}, {0, 3}, {8, 8}, {0, 4}, {8, 
1}, {0, 4}, {8, 2}, {0, 4}, {8, 3}, {0, 4}, {8, 2}, {0, 4}, {8, 
4}, {0, 4}, {8, 5}, {0, 4}, {8, 6}, {0, 4}, {8, 7}, {0, 4}, {8, 
8}, {0, 5}, {8, 1}, {0, 5}, {8, 2}, {0, 5}, {8, 3}, {0, 5}, {8, 
2}, {0, 5}, {8, 4}, {0, 5}, {8, 5}, {0, 5}, {8, 6}, {0, 5}, {8, 
7}, {0, 5}, {8, 8}, {0, 6}, {8, 1}, {0, 6}, {8, 2}, {0, 6}, {8,
3}, {0, 6}, {8, 2}, {0, 6}, {8, 4}, {0, 6}, {8, 5}, {0, 6}, {8,
6}, {0, 6}, {8, 7}, {0, 6}, {8, 8}, {0, 7}, {8, 1}, {0, 7}, {8,
2}, {0, 7}, {8, 3}, {0, 7}, {8, 2}, {0, 7}, {8, 4}, {0, 7}, {8,
5}, {0, 7}, {8, 6}, {0, 7}, {8, 7}, {0, 7}, {8, 8}, {0, 8}, {8,
1}, {0, 8}, {8, 2}, {0, 8}, {8, 3}, {0, 8}, {8, 2}, {0, 8}, {8,
4}, {0, 8}, {8, 5}, {0, 8}, {8, 6}, {0, 8}, {8, 7}, {0, 8}, {8, 8}};

lho = {Line[{{0, 0.5}, {9, 0.5}}], Line[{{0, 1.5}, {9, 1.5}}], 
   Line[{{0, 2.5}, {9, 2.5}}], Line[{{0, 3.5}, {9, 3.5}}], 
   Line[{{0, 4.5}, {9, 4.5}}], Line[{{0, 5.5}, {9, 5.5}}], 
   Line[{{0, 6.5}, {9, 6.5}}], Line[{{0, 7.5}, {9, 7.5}}], 
   Line[{{0, 8.5}, {9, 8.5}}]};

lver = {Line[{{0, 0}, {0, 9}}], Line[{{1, 0}, {1, 9}}], 
   Line[{{2, 0}, {2, 9}}], Line[{{3, 0}, {3, 9}}], 
   Line[{{4, 0}, {4, 9}}], Line[{{5, 0}, {5, 9}}], 
   Line[{{6, 0}, {6, 9}}], Line[{{7, 0}, {7, 9}}], 
   Line[{{8, 0}, {8, 9}}]};
$\endgroup$
2
  • $\begingroup$ At the very least, provide pts. $\endgroup$ Commented Apr 10, 2018 at 22:16
  • $\begingroup$ gladly! Updated $\endgroup$ Commented Apr 10, 2018 at 22:17

1 Answer 1

1
$\begingroup$

I am not sure but I think it does what you ask for. I just create a constant list lineweights of weights for the lines. (I would discourage storing the weight within the Line object in any way.)

lines = Flatten[Table[Line[{{0, i}, {8, j}}], {i, 1, 8}, {j, 1, 8}]];
lineweights = ConstantArray[1., Length[lines]];
rectangles = Flatten[Outer[
    Rectangle @@ Transpose[List[##]] &, 
    Partition[N[lver[[All, 1, 1, 1]]], 2, 1], 
    Partition[lho[[All, 1, 1, 2]], 2, 1], 
    1
  ]];
A = 1 - Outer[
  Boole@*RegionDisjoint, 
    DiscretizeRegion /@ lines, 
    rectangles
  ];
means = Mean[lineweights A];
Show[
 Graphics[Transpose[{GrayLevel /@ Rescale[1 - means], rectangles}]],
 Graphics[{Darker@Blue, lines}]
 ]

enter image description here

$\endgroup$
4
  • $\begingroup$ Thank you! I gave your code a try...from attempting to read/understand it...A is a count (list) of which lines are inside which squares? I'm not sure the given values can be true...When running the code several times the results ( changed RandomReal to 1-4 ) are generally less than 1. I would expect the means in all squares to be atleast 1 or higher. $\endgroup$ Commented Apr 11, 2018 at 8:25
  • $\begingroup$ The results change because the lineweights were randomized. I changed thit. means[[i]] belongs to reactangle[[i]]. I also added a plot. $\endgroup$ Commented Apr 11, 2018 at 8:42
  • $\begingroup$ Ahh I think maybe there is a confusion in my description. If in this case each lineweight is equal to one, and in each square, every segment of line is also equal to one, and an average of each is built, for example in the bottom left corner, it has 8 line segments, each value one, so 8*1 / 8....I expect 1 as the mean.... It looks here that the density of line partitions within each square goes up as we reach the centre, which makes sense, but unfortunately not the means I'm looking for :( But thank you! $\endgroup$ Commented Apr 11, 2018 at 8:55
  • $\begingroup$ Maybe it's means = Map[Mean[DeleteCases[#, 0.]] &, lineweights A] then? $\endgroup$ Commented Apr 11, 2018 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.