0
$\begingroup$

In the data below, I solved a heat transfer equation which gave me the Temperature for a given position and time. I want to make a 2D plot as a function of Temperature and position with the possibility of using a slider bar to show the changes of the plot with respect to time.

position = Table[j, {j, 0, 0.02, 0.0050}]; 
time = Table[i, {i, 0, 10, 1}]; 
Temperature={
 (*time0:*){20, 20, 20, 20, 20},
 (*time1:*){20.2026, 20.2018, 20.1994, 20.1806, 20.0291},
 (*time2:*){20.4062, 20.4022, 20.3904, 20.3268, 20.0509},
 (*time3:*){20.6097, 20.5998, 20.5698, 20.4517, 20.0687},
 (*time4:*){20.8109, 20.7927, 20.738, 20.5621, 20.0839},
 (*time5:*){21.0077, 20.9798, 20.8961, 20.6619, 20.0973},
 (*time6:*){21.1988, 21.1604, 21.0452, 20.7535, 20.1094},
 (*time7:*){21.3833, 21.3341, 21.1864, 20.8387, 20.1205},
 (*time8:*){21.5609, 21.5007, 21.3203, 20.9185, 20.1308},
 (*time9:*){21.7313, 21.6604, 21.4476, 20.9936, 20.1404},
 (*time10:*){21.8946, 21.8131, 21.5687, 21.0646, 20.1494}
};

I am trying to make a code similar below:

tLength=Length[time];
posLength=Length[position];

Manipulate[
 ListPlot[
  Table[{position[[j]], Temperature[[time, j]]}, {j, 1, posLength}]], {time, 1, 
  tLength}(*slide for time*)]

It would be additional knowledge for me too if you can show me how to implement a 3D plot for these data :)

$\endgroup$

1 Answer 1

1
$\begingroup$

You need to concatenate the data together into {time,position,temperature} triples. Table is your friend here for starters.

position=Table[j,{j,0,0.02,0.0050}];
time=Table[i,{i,0,10,1}];
Temperature=...;

data=Table[{time[[i]],position[[j]],Temperature[[i,j]]},{i,1,Length[time]},{j,1,Length[position]}];
data=Flatten[data,1]; (*this is to reformat your list into the form {{t1,p1,T1},{t2,p2,T2},..}*)

You can then easily plot this:

ListDensityPlot[data]
ListPlot3D[data]

enter image description here

The 3d plot looks a bit blocky because your data resolution is not very high, but an interpolation of your data can fix this easily:

dataInterpolation=Interpolation[data];
Plot3D[dataInterpolation[t,p],{t,0,10},{p,0,0.02}]

enter image description here

EDIT:

To do it with a manipulate, you have to go to a 1d plot:

Manipulate[
 ListLinePlot[Transpose[{position, Temperature[[Round@t]]}]], {t, 1, 
  Length@time}]

Which is what you want I guess? However, due to the limited resoltion, this does not look nice. So we Use the interpolation from before:

data = Table[{time[[i]], position[[j]], Temperature[[i, j]]}, {i, 1, 
    Length[time]}, {j, 1, Length[position]}];
data = Flatten[data, 
   1]; (*this is to reformat your list into the form \
{{t1,p1,T1},{t2,p2,T2},..}*)
dataInterpolation = Interpolation[data, InterpolationOrder -> 1];
Manipulate[
 Plot[dataInterpolation[t, p], {p, 0, 0.02}, 
  PlotRange -> {All, {19, 22}}], {t, Min[time], Max[time]}]

enter image description here

$\endgroup$
3
  • $\begingroup$ These look great! Thank you for your advice on how to fix my resolution too. I've learned so much from your comment. Is it also possible to indicate on the plot the time that is plotted when the slider bar is moved? something like a [Time = xx seconds] indicator on the plot? $\endgroup$
    – Mule
    Nov 2, 2023 at 12:35
  • 1
    $\begingroup$ You're welcome. Sure, just use the PlotLabe option for plot for example. Or place text via the Epilog option. Some examples: Plot[..,..,PlotLabel -> "Time t=" <> ToString[t]]. Or via epilog: Plot[..,..,Epilog -> Text["Time t=" <> ToString[t], {0.015, 21.5}]]] or more fancy with a little bit fixed formatting: Epilog->Text[Style["Time t="<>StringPadRight[ToString[t],4],{FontSize->14,FontFamily->"Consolas"}],{0.015,21.5}]. Hope this helps you as well. $\endgroup$ Nov 2, 2023 at 12:54
  • $\begingroup$ exactly what I asked for and you gave me more options. Thank you very much for your help! :D $\endgroup$
    – Mule
    Nov 2, 2023 at 13:05

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.