How to draw multiple triangles in python

Using a PolyCollection [as shown in @cripcate's answer] is advantageous in this case. A more condensed version using a single numpy array could look like this:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection

def triangle_collection[d, ax=None, width=0.4, **kwargs]:
    ax = ax or plt.gca[]
    verts = np.c_[d[:,0]-width/2, d[:,1], d[:,0]+width/2, 
                  d[:,1], d[:,0], d[:,2]].reshape[len[d],3,2]
    c = PolyCollection[verts, **kwargs]
    ax.add_collection[c]
    ax.autoscale[]
    return c


data = np.array[[[1,22,20], [2,21,19.5], [3,18,20],
                 [4,17,19], [5,15,17], [6,11,8.5], [7,14,12]]]

fig, ax = plt.subplots[]
fig.subplots_adjust[left=0.3, right=0.7]

triangle_collection[data, facecolors=plt.cm.tab10[np.arange[len[data]]]]

plt.show[] 

Drawing with Turtle in Python is really fun. In the past handful of tutorials, we learned how to import the Turtle module for use in our programs saw how to make the turtle [pen] move on the canvas, made the turtle change directions on the canvas, saw how to use loops in turtle, and made drawings of shapes using variables. Now we will take a look at drawing another type of Polygon, the triangle using the Turtle library in Python.

Define A Triangle Function

To draw a triangle, we want to use a function, and it should make use of variables. We do this so that we can call the function many times if we like to draw many triangles of different sizes.

from turtle import *

drawing_area = Screen[]
drawing_area.setup[width=750, height=500]
shape['triangle']


def draw_triangle[length=150]:
    for i in range[3]:
        forward[length]
        left[120]


draw_triangle[]

done[]

Nice! The triangle function works. Notice that the loop uses 3 iterations shown by range[3] whereas when we drew a square it used 4 iterations in the loop. Another thing we notice is that we are passing in 120 as the degrees to turn for the left[] function. Why is that? This is because when drawing a triangle, you need to use the outside angle rather than the inside angle. What we are drawing here is an equilateral triangle since all three angles of an equilateral triangle add up to 180 degrees.

Drawing More Triangles

Now we can use the draw_triangle[] function a couple of times in combination with moving the turtle to a different spot on the canvas for a nice effect.

from turtle import *

drawing_area = Screen[]
drawing_area.setup[width=750, height=500]
shape['triangle']


def draw_triangle[length=150]:
    for i in range[3]:
        forward[length]
        left[120]


draw_triangle[]
right[180]
forward[100]
right[180]
draw_triangle[200]

done[]

The following iteration takes that idea a step further to draw three triangles in different spots on the canvas.

from turtle import *

drawing_area = Screen[]
drawing_area.setup[width=750, height=500]
shape['triangle']


def draw_triangle[length=150]:
    for i in range[3]:
        forward[length]
        left[120]


draw_triangle[]
right[180]
forward[100]
right[180]
draw_triangle[200]
right[180]
forward[100]
right[180]
draw_triangle[250]

done[]

Drawing Triangles In A Loop

Calling the draw_triangle[] inside of a loop makes for some really cool effects.

from turtle import *

drawing_area = Screen[]
drawing_area.setup[width=750, height=500]
shape['triangle']


def draw_triangle[length=150]:
    for i in range[3]:
        forward[length]
        left[120]


for i in range[20]:
    draw_triangle[]
    right[1]

done[]

from turtle import *

drawing_area = Screen[]
drawing_area.setup[width=750, height=500]
shape['triangle']


def draw_triangle[length=150]:
    for i in range[3]:
        forward[length]
        left[120]


for i in range[40]:
    draw_triangle[]
    right[10]

done[]

How do you draw a triangle in Python?

How to Draw a Triangle in Python Turtle.
Draw a line with pen - forward[] command..
Move without drawing - penup[], pendown[] commands..
Turn the pen to an angle - left[], right[] commands..

How do you make a for loop in a triangle in Python?

def triangle[n]:.
for i in range[1, n+1]:.
print[' ' * n, end=''].
print['* '*[i]].
n -= 1..
n = int[input[]].
triangle[n].

How do you make a spiral triangle turtle in Python?

To draw something on the screen[cardboard] just move the turtle[pen]..
Import turtle and create a turtle instance..
Using for loop[i = 0 to i< n * 3] and repeat below step. turtle. forward[i * 10]. turtle. right[120]..
Close the turtle instance..

Chủ Đề