我正在嘗試可視化每個 X 值具有多個 Y 值的資料,并且我想區分每個 Y 值。這是示例代碼
xLables = ['A1','A2','A3','A4','A5']
YValues = [[1,2,3,4],[1,2,3,4,5,6,7],[1,2,3],[5,6,7],[1,2,3]]
X = [xLables[i] for i, data in enumerate(YValues) for j in range(len(data))]
Y = [val for data in YValues for val in data]
plt.scatter(X, Y)
plt.grid()
plt.show()
當我繪制這個時,我附上了以下內容

每個 X 標簽都有對應的 Y 值...例如:A1 有 1,2,3,4 ,A2 有 1,2,3,4,5,6,7 等等
我對此有兩個問題
(1) 我可以為不同的 Y 值設定不同的標記嗎?所有 1 都是星星,所有 2 都是菱形,所有 10 都是圓圈?
像這樣的東西可能是

(2) 有沒有更好的方法來繪制這樣的二維資料并在每個 X 有多個 Y 值的情況下區分它們
非常感謝任何建議/幫助
謝謝
我嘗試添加標記和不同的顏色,但它們適用于每個 X 的所有 Y 值 .. 但不是特定于每個 Y 值..
uj5u.com熱心網友回復:

我的解決方案特別特別,但它使用您的資料復制了您的目標繪圖,因此我有信心將她發布在這里。
import matplotlib.puplot as plt
labels = ['A1','A2','A3','A4','A5']
Y2D = [[1,2,3,4],[1,2,3,4,5,6,7],[1,2,3],[5,6,7],[1,2,3]]
# prepare a dictionary with the characteristics
# we want to change according to the Y value
d = {1:dict(marker="*", s=150, color="red"),
2:dict(marker="o", s=100, color="yellow"),
3:dict(marker="o", s= 60, color="blue"),
4:dict(marker="o", s=100, color="green"),
5:dict(marker="o", s=100, color="red"),
6:dict(marker="*", s=150, color="blue"),
7:dict(marker="o", s=100, color="lightgray")}
# an outer loop on the abscissae and the lists of Y values
for x, ys in zip(labels, Y2D):
an inner loop on the Y values, plotted separately
for y in ys:
# here the point is to unpack the values contained
# in the "inner" dictionary, addressing the outer by Y
# zorder=5 places the dots above the grid
plt.scatter(x, y, ec='k', zorder=5, **d[y])
plt.grid(1)
plt.show()
uj5u.com熱心網友回復:
將您的標記型別添加到串列并相應地迭代它們。
from matplotlib import pyplot as plt
import matplotlib
xLables = ['A1','A2','A3','A4','A5']
YValues = [[1,2,3,4],[1,2,3,4,5,6,7],[1,2,3],[5,6,7],[1,2,3]]
X = [xLables[i] for i, data in enumerate(YValues) for j in range(len(data))]
Y = [val for data in YValues for val in data]
plt.scatter(X, Y, marker=matplotlib.markers.CARETDOWNBASE)
plt.grid()
markers=['8',' ', '.', 'o', '*','^', 's', 'p', 'h','8',' ', '.', 'o', '*','^', 's', 'p', 'h' ]
for i in range(18):
plt.plot(X[i], Y[i], marker=markers[i])
plt.xlabel('X Label')
plt.ylabel('Y Label')
plt.show()
輸出 #

注意:根據需要更改您想要的順序。這將在圖表上復制
uj5u.com熱心網友回復:
您可以添加標記。可以在此處找到它們的串列:https ://matplotlib.org/stable/api/markers_api.html
我認為您可以像這樣使用它:
plt.scatter([1, 2, 3], marker=11) 這意味著您必須將想要相同的值放在同一個串列中。我認為沒有辦法給出標記串列或類似的東西。
據我了解,您的代碼如下所示:
plt.scatter(X, Y[0], marker=1)
plt.scatter(X, Y[1], marker=1)
plt.scatter(X, Y[2], marker=1)
plt.scatter(X, Y[3], marker=1)
如果它必須適用于不同大小的東西,您可以將其設為 for 回圈,但我想您會從這里弄清楚。
祝你好運,我希望這會有所幫助。
uj5u.com熱心網友回復:
我認為最簡單的方法是每個分數使用一個 plt.scatter() 。
import matplotlib.pyplot as plt
xLables = [ 'A1','A2','A3','A4','A5']
YValues = [ [1,2,3,4],[1,2,3,4,5,6,7],[1,2,3],[5,6,7],[1,2,3]]
markers = [ '.', 'o', '^', 'v', '>', '<', '*'] # to be customized
Y = [None for i in range( len( xLables))]
for y in range( len( markers)):
for x in range( len( xLables)):
Y[x] = y 1 if y 1 in YValues[x] else None # values start at 1
if any( Y): # something to display?
plt.scatter( xLables, Y, marker=markers[y])
plt.grid()
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/532755.html
上一篇:收到錯誤時TWSAPI凍結
下一篇:Matplotlib軸上的可讀值
