HowardNTUST / Python-with-STP-Marketing-Strategy

STP 行銷策略之 Python 商業應用實戰
15 stars 12 forks source link

雷達圖1/2 in 盤點自身市場定位.ipynb執行時會報ValueError: The number of FixedLocator locations (10), .... #5

Closed paulyang0125 closed 2 years ago

paulyang0125 commented 2 years ago

在我環境中雷達圖會報以下錯誤在ax.set_thetagrids(angles * 180/np.pi, all_keyword). 我解決方法是將all_keyword改成numpy array 並調整總數為10

# 構造數據
for da,c,ab in zip(container1, colorcompetitor, all_board):
    value=np.concatenate((np.array(da), np.array([da[0]])),axis=0)
    ax.plot(angles, value, 'o-', linewidth=2, label = ab,color=c)# 繪製折線圖
    ax.fill(angles, value, alpha=0.25,color=c)# 填充顏色

+ # 改成np array並調整數量為10 
+ all_keyword = np.concatenate((all_keyword,[all_keyword[0]]))

ax.set_thetagrids(angles * 180/np.pi, all_keyword) # 添加每個特徵的標籤
ax.set_ylim(0,1)# 設置雷達圖的範圍

執行時完整錯誤:


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-225-639a71933a75> in <module>
     28 ###
     29 
---> 30 ax.set_thetagrids(angles * 180/np.pi, all_keyword) # 添加每個特徵的標籤
     31 ax.set_ylim(0,1)# 設置雷達圖的範圍
     32 

~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/projections/polar.py in set_thetagrids(self, angles, labels, fmt, **kwargs)
   1344         self.set_xticks(angles)
   1345         if labels is not None:
-> 1346             self.set_xticklabels(labels)
   1347         elif fmt is not None:
   1348             self.xaxis.set_major_formatter(mticker.FormatStrFormatter(fmt))

~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py in wrapper(self, *args, **kwargs)
     61 
     62         def wrapper(self, *args, **kwargs):
---> 63             return get_method(self)(*args, **kwargs)
     64 
     65         wrapper.__module__ = owner.__module__

~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/cbook/deprecation.py in wrapper(*args, **kwargs)
    449                 "parameter will become keyword-only %(removal)s.",
    450                 name=name, obj_type=f"parameter of {func.__name__}()")
--> 451         return func(*args, **kwargs)
    452 
    453     return wrapper

~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axis.py in _set_ticklabels(self, labels, fontdict, minor, **kwargs)
   1791         if fontdict is not None:
   1792             kwargs.update(fontdict)
-> 1793         return self.set_ticklabels(labels, minor=minor, **kwargs)
   1794 
   1795     @cbook._make_keyword_only("3.2", "minor")

~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axis.py in set_ticklabels(self, ticklabels, minor, **kwargs)
   1712             # remove all tick labels, so only error for > 0 ticklabels
   1713             if len(locator.locs) != len(ticklabels) and len(ticklabels) != 0:
-> 1714                 raise ValueError(
   1715                     "The number of FixedLocator locations"
   1716                     f" ({len(locator.locs)}), usually from a call to"

ValueError: The number of FixedLocator locations (10), usually from a call to set_ticks, does not match the number of ticklabels (9).
derek890111 commented 2 years ago

同學您好: 非常感謝您的回覆,會出現此error應該是您的matplotlib版本在繪製雷達圖時,若角度數與標籤數不同將無法執行。您可以在該行程式碼前新增「angles=angles[:-1]」,即可繪製雷達圖,操作畫面如下圖所示: 若還有遇到疑問,歡迎在下方留言!非常感謝~ angles

paulyang0125 commented 2 years ago

了解 謝謝您