0

I am a beginner at this, and I am needing to make a triangle and have it graphed on my VM in Linux. Using Python, the first step would be to generate just three random points for the triangle. How would be the best way to accomplish this? Any help, tips, or advice would be appreciated!

Seth
  • 13
  • 5
  • you want to use matplotlib or you just want to draw any triangle? – Keatinge Apr 05 '16 at 16:46
  • this is two separate problems. you need to know 1) how to generate random data and 2) how to plot an area. there are lots of questions about both already. – Paul H Apr 05 '16 at 16:51
  • here's a post about generating random values: http://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9 – Paul H Apr 05 '16 at 16:51
  • and here's a post about filling in a polygon http://stackoverflow.com/questions/8919719/how-to-plot-a-complex-polygon – Paul H Apr 05 '16 at 16:51
  • @Racialz I just want to draw any triangle – Seth Apr 05 '16 at 18:16

2 Answers2

2

Using matplotlib as indicated in the tags:

import matplotlib.pyplot as plt
data = [[6, 2, 3], [1, 2, 3]]

plt.plot(data[0] + [data[0][0]], data[1] + [data[1][0]], marker='o', color='blue')
plt.show()

To generate random data: (look up the random module to choose the appropriate generator)

import random
data = [[random.randrange(10) for _ in range(3)], [random.randrange(10) for _ in range(3)]]

enter image description here

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • this works well for me...thank you! Is there any way to plot it using a Tk window instead? – Seth Apr 05 '16 at 18:15
  • Excellent, glad I could help. Look here to see code examples than embed matplotlib plots in a tkinter frame: http://matplotlib.org/examples/user_interfaces/index.html – Reblochon Masque Apr 05 '16 at 18:17
-1

With pylab commands (Numpy and Matplotlib) you can do:

>>> a = rand(3, 2); a = vstack((a, a[0])); plot(a[:, 0], a[:, 1]); show()

You need something like python -i -c 'from pylab import *' to get the Numpy and Matplotlib functions in the namespace.

Finn Årup Nielsen
  • 6,130
  • 1
  • 33
  • 43
  • I see a downvote on my answer and wonder why? The line uses standard pylab commands. Start Python with `python -i -c "from pylab import *"` or IPython with `ipython -pylab`. If you have Numpy and Matplotlib installed it should work. – Finn Årup Nielsen Apr 06 '16 at 09:46
  • 1
    I didn't downvote, and your code may well work, but in general its best to give some context/explanation rather than code-only answers, to improve the long-term value of your answer. – tmdavison Apr 06 '16 at 09:55
  • Thanks for the explanation. I have expanded the answer with comments. – Finn Årup Nielsen Apr 06 '16 at 14:22