我可以這樣做并產生分散嗎?
y-axis tick/label order → [0, 7, 2, 9, 4, 11, 6, 1, 8, 3, 10, 5]
x = ('a', 'b', 'c', 'd', 'e')
y = [[1,5,2], [10,5,11,7], [9], [], [ 7, 2, 9, 4, 11, 6, 1, 8, 3, 10, 5]]
這個想法是將 x 值映射到 y 值。例如,第一資料的串列('a')是,比方說,打1,5和2上的目標,所述第二('b')擊中分數10,5,11,7。我需要保持Y軸作為標簽順序顯示。

uj5u.com熱心網友回復:
該實作包括ticklabels按 定義的順序獲取 y 軸yticks,這需要將 更改為yticklabels與默認 ytick 坐標不匹配的內容。
import matplotlib.pyplot as plt
x_labels = ("a", "b", "c", "d", "e")
yticks = (0, 7, 2, 9, 4, 11, 6, 1, 8, 3, 10, 5)
# y values to be plotted
y_lists = ([1, 5 , 2], [10, 5, 11, 7], [9], [], [7, 2, 9, 4, 11, 6, 1, 8, 3, 10, 5])
# Define the figure and ax.
fig, ax = plt.subplots(figsize=(8, 5))
for x, y_list in zip(x_labels, y_lists):
# We have a single x value for each letter, but we need the x-list to be as long y-list in order to make a scatter.
x_list = [x]*len(y_list)
# Notice the use of `.index` here to accommodate the ticks not being ordered.
true_y_list = [yticks.index(y) for y in y_list]
ax.scatter(x_list, true_y_list)
# set the sorted y-ticks.
ax.set_yticks(sorted(yticks))
ax.set_yticklabels(yticks)
# set the x-ticks because the API will not show D on the axis since it has no y values
ax.set_xticks(range(len(x_labels)))
ax.set_xticklabels(x_labels) # `ax.set_xticklabels("abcde")` would work too.
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331257.html
標籤:Python matplotlib 散点图
上一篇:在Wicket9中,當用戶的會話在某些頁面中過期時,他們被重定向到登錄頁面而不是會話過期頁面
下一篇:如何在多個散點之間放置箭頭
