[英]Matplotlib `fill_between` to fill in rhomboid
我目前正在尝试使用matplotlib.pyplot.fill_between()
方法在看起来像菱形的区域中着色。
我当前使用fill_between()
两次,但我想知道是否有一种方法可以只使用一次方法来填充形状。 请允许我详细说明。
为了得到以下图像:
我编写的代码如下所示:
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()
有没有一种方法可以只使用一次fill_between()
方法来填充形状?
谢谢。
由于您正在绘制多边形,因此请考虑绘制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.