2
$\begingroup$

I'm trying to generate a set of cylinders/tubes with different radii and colours.

In particular, I have this graph: {1 -> 2, 2 -> 3, 3 -> 4, 4 -> 5, 5 -> 1} with the following vertexes {0, 0, 0}, {0, 0, 10}, {10, 0, 10}, {10, 10, 0}, {5, 15, 10}.

Furthermore, I have a list of values b = {1, 3, 0.5, -1, -2} and I want to plot 5 tubes (one for each value) with a specified radius r1 if the related element of b is greater than zero, and r2 if it is <0. How can I do this? Also, I want to plot these tubes with a range of colours consistent with the vector b and a framed bar legend. Thanks.

q = {1, 3, 0.5, -1, -2};
Grafo = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 5, 5 <-> 1}, 
EdgeWeight -> q];
el = EdgeList[Grafo];
edgestylea = 
Thread[el -> (Directive[
ColorData[{"TemperatureMap", {Min[q], Max[q]}}][#]] & /@ q)];
Legended[Graph3D[Grafo, EdgeStyle -> edgestylea, 
EdgeShapeFunction -> ({ColorFunction -> (ColorData[
"TemperatureMap", #] & /@ q), 
Cylinder[#, If[# < 0, 0.1, 0.5] & /@ q]} &), 
VertexLabels -> "Name", 
VertexShapeFunction -> ({Yellow, Sphere[#, 1]} &), 
VertexCoordinates -> {{0, 0, 0}, {0, 0, 10}, {10, 0, 10}, {10, 10, 
0}, {5, 15, 10}}, AxesOrigin -> {0, 0, 0}, Axes -> True, 
Ticks -> Automatic, 
PlotLabel -> 
Style[Framed["Tensegrity", RoundingRadius -> 10], 14, Black, 
FontFamily -> "Times New Roman"], AxesLabel -> {x, y, z}, 
LabelStyle -> {FontFamily -> "Times New Roman"}], 
Framed[BarLegend[{"TemperatureMap", {Min[q], Max[q]}}, 
LegendLabel -> "Internal force" , 
LabelStyle -> {FontFamily -> "Times New Roman"}], 
RoundingRadius -> 10, FrameStyle -> GrayLevel[.5]]]

In my code, tubes with different radii consistent with b don't work! Someone can help me?

$\endgroup$
6
  • 1
    $\begingroup$ What about the axes of these cylinders? $\endgroup$ Commented Apr 11, 2017 at 14:13
  • $\begingroup$ I have this graph {1 [DirectedEdge] 2, 2 [DirectedEdge] 3, 3 [DirectedEdge] 4,4 [DirectedEdge] 5,5 [DirectedEdge] 6} with the following vertexes {0, 0, 0}, {0, 0, 10}, {10, 0, 10}, {10, 10, 0},{10, 10, 10},{10, 10, 20}. $\endgroup$
    – Gae P
    Commented Apr 11, 2017 at 15:27
  • $\begingroup$ You should've mentioned that to begin with. Please edit your question to include this information. $\endgroup$ Commented Apr 11, 2017 at 15:34
  • $\begingroup$ Sorry. I have this graph: {1 [DirectedEdge] 2, 2 [DirectedEdge] 3, 3 [DirectedEdge] 4,4 [DirectedEdge] 5,5 [DirectedEdge] 6} with the following vertexes {0, 0, 0}, {0, 0, 10}, {10, 0, 10}, {10, 10, 0},{10, 10, 10},{10, 10, 20}. I have a list of values b={1,3,0.5,-1,-2} and I want to plot 5 tubes (one for each value) with a specified radius r1 if the related element of b is greater than zero, and r2 if it is <0. How can I do this? Also, I want to plot the tubes with a range of colours consistent with the vector b and a framed bar legend. Thanks $\endgroup$
    – Gae P
    Commented Apr 12, 2017 at 7:15
  • $\begingroup$ Please use the "edit" button at the bottom of your post, or click on this link, to add information to your post, instead of keeping it in the comments. $\endgroup$ Commented Apr 12, 2017 at 7:19

1 Answer 1

1
$\begingroup$
b = {1, 3, 0.5, -1, -2};

SeedRandom[3];

data = Transpose[
   {Partition[RandomReal[1, {6, 3}], 2, 1], b}];

Graphics3D[Tube[#[[1]], If[#[[2]] < 0, 0.025, 0.05]] & /@ data]

enter image description here

or

r[x_] = Piecewise[{{0.025, x < 0}}, 0.05];

Graphics3D[Tube[#[[1]], r[#[[2]]]] & /@ data]

EDIT:

vertices = {{0, 0, 0}, {0, 0, 10}, {10, 0, 10}, {10, 10, 0}, {10, 10, 
    10}, {10, 10, 20}};

Graphics3D[{Red, Arrowheads[0.075],
  Arrow[Tube[#[[1]], If[#[[2]] < 0, 0.1, 0.25]]] & /@ 
   Transpose[{Partition[vertices, 2, 1], b}]},
 Boxed -> False]

enter image description here

$\endgroup$
1
  • $\begingroup$ I have this graph {1 [DirectedEdge] 2, 2 [DirectedEdge] 3, 3 [DirectedEdge] 4,4 [DirectedEdge] 5,5 [DirectedEdge] 6} with the following vertexes {0, 0, 0}, {0, 0, 10}, {10, 0, 10}, {10, 10, 0},{10, 10, 10},{10, 10, 20}. $\endgroup$
    – Gae P
    Commented Apr 11, 2017 at 15:13

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.