My First Python Drawing

Idea: I want to draw a simple shape using Python.

Question to DeepSeek:

Draw an equilateral triangle with a side length of 3cm in Python.

The response given was, after trying left and right, I am still very confused.

Python draws a 3cm equilateral triangle

Python draws a 3cm equilateral triangle

Python draws an equilateral triangle with a side length of 3cm

Python draws an equilateral triangle

Here is the code to draw an equilateral triangle with a side length of 3cm using the Python matplotlib library:

python

import matplotlib.pyplot as plt

import numpy as np

# Set the figure size to ensure the display ratio is correct

plt.figure(figsize=(6, 6))

# Calculate the vertex coordinates of the equilateral triangle

# Assume the base is horizontally placed, with the two vertices of the base at

(-1.5, 0) and (1.5, 0)

# The height of the equilateral triangle

height = side_length * np.sqrt(3) / 2 # ≈ 2.598

side_length = 3

height = side_length * np.sqrt(3) / 2

# Vertex coordinates

x = [-side_length/2, side_length/2, 0, -side_length/2]

y = [0, 0, height, 0]

# Draw the triangle

plt.plot(x, y, ‘b-‘, linewidth=2)

# Set the axis aspect ratio to equal to ensure the shape is not distorted

plt.axis(‘equal’)

# Set the axis limits

plt.xlim(-2, 2)

plt.ylim(-0.5, 3)

# Add title and labels

plt.title(f’Equilateral Triangle (Side Length = {side_length}cm)’)

plt.xlabel(‘x (cm)’)

plt.ylabel(‘y (cm)’)

# Add grid

plt.grid(True, linestyle=’–‘, alpha=0.7)

# Show the figure

plt.show()

This code will draw an equilateral triangle with a side length of 3cm and ensure the display ratio is correct so that the triangle does not appear distorted.

If you want to represent the length of 3cm more accurately, you can use the conversion features of matplotlib, but be aware that the actual size displayed on the screen will be affected by the display device’s DPI.

Then I had to continue asking:

Where should I paste this code?

The answer is as follows:

This Python code can be pasted and run in several places:

1. Jupyter Notebook / JupyterLab (recommended)

Installation:

pip install jupyterlab

Start:

jupyter lab

Then create a new Notebook file and paste the code in a code cell to run it.

2. Python script file

Create a .py file (e.g., triangle.py)

Paste the code and save it.

Run it in the terminal/command line:

python triangle.py

3. Online Python environment

Google Colab: https://colab.research.google.com/

Replit: https://replit.com/

Paste and run directly.

4. Python IDE

PyCharm

VS Code (requires Python extension)

Spyder

Thonny

5. Simple online running method

If you want to run it immediately, here is a simplified version of the online code:

python

# Run at https://www.python.org/shell/ or similar online environments

import matplotlib.pyplot as plt

import numpy as np

side_length = 3

height = side_length * np.sqrt(3) / 2

x = [-side_length/2, side_length/2, 0, -side_length/2]

y = [0, 0, height, 0]

plt.plot(x, y, ‘b-‘, linewidth=2)

plt.axis(‘equal’)

plt.title(f’Equilateral Triangle (Side Length = {side_length}cm)’)

plt.grid(True)

plt.show()

It is recommended to use Jupyter Notebook as it is best suited for data visualization and learning Python. You need to install the necessary libraries first:

bash

pip install matplotlib numpy

Which method do you want to use to run it? If you need more detailed installation guidance, please let me know!

After trying a few more times, I continued to ask:

I already have Python and the VS CODE extension, what else do I need to install to draw?

The answer:

To run drawing code in VS Code, you need to install the following libraries:

Required library installation

Open the terminal in VS Code and run the following commands:

bash

# Install matplotlib (drawing library)

pip install matplotlib

# Install numpy (mathematical computation library)

pip install numpy

Steps to run in VS Code

Create a new Python file:

Create a new file and save it as triangle.py

Paste the complete code:

python

import matplotlib.pyplot as plt

import numpy as np

# Set Chinese font (to avoid Chinese display issues)

plt.rcParams[‘font.sans-serif’] = [‘SimHei’] # For normal display of Chinese labels

plt.rcParams[‘axes.unicode_minus’] = False # For normal display of negative signs

# Set figure size

plt.figure(figsize=(6, 6))

# Calculate the vertex coordinates of the equilateral triangle

side_length = 3

height = side_length * np.sqrt(3) / 2

# Vertex coordinates

x = [-side_length/2, side_length/2, 0, -side_length/2]

y = [0, 0, height, 0]

# Draw the triangle

plt.plot(x, y, ‘b-‘, linewidth=2)

# Set the axes

plt.axis(‘equal’)

plt.xlim(-2, 2)

plt.ylim(-0.5, 3)

# Add title and labels

plt.title(f’Equilateral Triangle (Side Length = {side_length}cm)’)

plt.xlabel(‘x (cm)’)

plt.ylabel(‘y (cm)’)

plt.grid(True, linestyle=’–‘, alpha=0.7)

# Show the figure

plt.show()

Run the code:

Press F5, or click the run button in the upper right corner, or

type in the terminal:

python triangle.py

If you encounter issues

If Chinese displays as boxes, install additional fonts:

bash

pip install matplotlib-font-manager

If it prompts that the module is not found, check the Python interpreter:

Press Ctrl+Shift+P

Type “Python: Select Interpreter”

Select the correct Python environment

After installation, you can run the drawing code in VS Code!

This time the answer gave me actionable steps, and following the prompts, I finally clicked the run button in the upper right corner of the VS CODE page, and after a while, a big triangle appeared. At that moment, I felt like I had found an oasis in the desert, and the joy after confusion surged. Actually, there was no specific purpose; I just wanted to use it while learning Python.

My First Python Drawing

Leave a Comment