Cedric H.
Cedric H.

Reputation: 8298

Transitioning to Python from Mathematica

I have the following piece of code to plot "resonance lines":

TuneDiagram[MyOrder_, MyColor_] := Module[{},
  myLines = 
   Partition[
    Flatten[Table[{{A -> a, B -> +MyOrder - a, C -> p}, {A -> a, 
        B -> -MyOrder + a, C -> p}}, {a, 0, MyOrder}, {p, -MyOrder, 
       MyOrder}]], 3];
  myEquation = A  x + B y == C /. myLines;
  ContourPlot[Evaluate[myEquation], {x, 0, 1}, {y, 0, 1}, 
   ContourStyle -> MyColor, PlotRangePadding -> None, 
   GridLines -> None, 
   ]]

I can obtain plots likes this one.

I would like to get the same result using Python / matplotlib.

I'm totally confused by the "translation" of this programming style onto Python. Any pointer would be much appreciated!

enter image description here

Upvotes: 0

Views: 260

Answers (1)

Edmund
Edmund

Reputation: 528

As per the OP's request here is a method to translate myLines into parametric equations. This might be easier to convert into Python.

pars = {x, y} /. DeleteDuplicates[
   First@Solve[#, First@Variables@*First@#] & /@ myLines];

Use Solve and Variables to get the parametric equation of the linear functions in myLines in one of the functions variables.

Then plot these with ParametricPlot. I'm guessing that Python can do a parametric plot.

Show[ParametricPlot[#, {x, 0, 1}, {y, 0, 1}] & /@ par,
 PlotRange -> {{0, 1}, {0, 1}}, PlotRangePadding -> None]

enter image description here

Hope this helps.

Upvotes: 2

Related Questions