簡體   English   中英

Matplotlib`fill_between`填充菱形

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM