简体   繁体   English

Matplotlib`fill_between`填充菱形

[英]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. 我目前正在尝试使用matplotlib.pyplot.fill_between()方法在看起来像菱形的区域中着色。

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. 我当前使用fill_between()两次,但我想知道是否有一种方法可以只使用一次方法来填充形状。 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? 有没有一种方法可以只使用一次fill_between()方法来填充形状?

Thank you. 谢谢。

Since you are drawing a polygon, consider drawing a Polygon . 由于您正在绘制多边形,因此请考虑绘制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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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