簡體   English   中英

Matplotlib自定義標記路徑

[英]Matplotlib Custom Marker Path

我正在嘗試使用Path實例在Matplotlib中創建自定義標記,如此處所述: https ://matplotlib.org/api/markers_api.html#module-matplotlib.markers

import matplotlib.pyplot as plt
import matplotlib as mpl

x = [1, 2, 3, 4]
y = [1, 4, 9, 6]

custommarker = mpl.path.Path([[0,0],[1,1],[1,0]],[1,2,2])
plt.plot(1.5,0,marker=custommarker)

plt.plot(x, y, 'ro')
plt.subplots_adjust(bottom=0.15)
plt.show()

當我運行代碼時,出現錯誤:

TypeError: 'Path' object does not support indexing

在mpl 1.3.x中,這是可行的,但是由於mpl 2.xx,我得到了這個錯誤。 誰能幫我? 非常感謝。

從代碼生成的完整錯誤消息中,可以看出該錯誤在實際的markers.py函數的set_marker函數中。 正如ImportanceOfBeingErnest在評論中指出的那樣,實際上這是一個已修復但尚未發布的錯誤(到目前為止,截至2018年12月4日),如當前markers.py主版本中所示。

引發錯誤的代碼如下:

if (isinstance(marker, np.ndarray) and marker.ndim == 2 and
        marker.shape[1] == 2):
    self._marker_function = self._set_vertices
elif (isinstance(marker, Sized) and len(marker) in (2, 3) and
        marker[1] in (0, 1, 2, 3)):
    self._marker_function = self._set_tuple_marker

它是不是直到一些elif后,對於檢查isinstance(marker,Path)進行。

一種解決方法是觸發第一個if ,以避免結束執行marker[1] 此條件檢查尺寸與路徑對象的頂點一致的numpy數組,而不是傳遞custommarker ,而傳遞其頂點:

plt.plot(1.5,0,marker=custommarker.vertices)

另一個選擇是if對標記使用不同的長度,則避免第二種選擇,因為只有len(marker) in (2,3)會產生錯誤:

custommarker = mpl.path.Path([(0,0),(1,1),(1,0),(1,0)],[1,2,2,79]) # Or a 0 as final code if instead o closing the line should stop there.

兩種解決方法均得出相同的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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