0

I tried to make a single polygon with the following code, but it made two?

import matplotlib.pyplot as plt

x = [4, 1, 2]
y = [1, 2, 1]
z = [0, 2, 1]

plt.fill(x, y, z)
plt.show()

This code displays two polygons in different colors, but I want one polygon with a single, unified color.

1 Answer 1

2

Just set the color of the polygons to be the same:

import matplotlib.pyplot as plt

x = [4, 1, 2]
y = [1, 2, 1]
z = [0, 2, 1]

plt.fill(x, y, z, c='C0')
plt.show()

Filled polygon

I'm not completely certain why the preceding code works like it does. plt.fill() is used for plotting 2D polygons, and the third argument should be the color, so what you should really write is this:

x = [4, 1, 0, 2]
y = [1, 2, 0, 1]

plt.fill(x, y, c='C0')
plt.show()

(which gives the same plot)

Filled polygon

Sign up to request clarification or add additional context in comments.

2 Comments

plt.fill(x,y,z) is the same as calling plt.fill(x,y); plt.fill(z). You can in fact draw as many filled shapes as you like, though for better readability I would propose to always define x and y, like plt.fill(x1,y1, x2,y2, x3, y3).
Thanks for the clarification!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.