简体   繁体   中英

Matplotlib `fill_between` to fill in rhomboid

I'm currently trying to color in a region that looks like a rhomboid using the matplotlib.pyplot.fill_between() method.

I'm currently using fill_between() twice but I was wondering if there was a way to fill in the shape using the method just once. Allow me to elaborate.

In order to get the following image:

在此处输入图片说明

The code I wrote looks like this:

import matplotlib.pyplot as plt

# Fill in area.
plt.fill_between([0, 1, 4], [0, 1, 4], [0, 3, 4], color='C2', label=r'$c\vec{v} + d\vec{w}$')
plt.fill_between([0, 3, 4], [0, 3, 4], [0, 1, 4], color='C2')

# v
plt.quiver(0, 0, 3, 1, color='C0', angles='xy', scale_units='xy', scale=1, label=r'$\vec{v}$')

# w
plt.quiver(0, 0, 1, 3, color='C1', angles='xy', scale_units='xy', scale=1, label=r'$\vec{w}$')

# Miscellaneous.
plt.xlim(-1, 6)
plt.ylim(-1, 6)
plt.axhline(0, color='black')
plt.axvline(0, color='black')
plt.xlabel(r'$x$', fontsize='large')
plt.ylabel(r'$y$', fontsize='large')
plt.grid()
plt.legend()
plt.show()

Is there a way to fill in the shape by using the fill_between() method just once?

Thank you.

Since you are drawing a polygon, consider drawing a Polygon .

import numpy as np
import matplotlib.pyplot as plt

# v
plt.quiver(0, 0, 3, 1, color='C0', angles='xy', scale_units='xy', scale=1, label=r'$\vec{v}$')
# w
plt.quiver(0, 0, 1, 3, color='C1', angles='xy', scale_units='xy', scale=1, label=r'$\vec{w}$')

verts = [0,3,4,1,0]
poly = plt.Polygon(np.c_[verts,verts[::-1]], color="C2", zorder=0,
                   label=r'$c\vec{v} + d\vec{w}$')
plt.gca().add_patch(poly)


# Miscellaneous.
plt.autoscale()
plt.margins(.2)
plt.grid()
plt.legend(loc="upper left")
plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM