[英]Having trouble assigning colors to a scatterplot (Python, Matplotlib)
我是 Python 的新手,並進行了一些練習。 我目前正在研究下面的散點圖。 我遇到的問題是我想像這樣給我的點上色:負=紅色,中性=藍色,正=綠色。
我將以下內容放在一起,並且“幸運”地找到了“plt.cm.brg”(藍色/紅色/綠色)顏色,因為我是根據我之前建立的圖表得出的。 幸運的是,colors 沒有按我想要的順序分配...
如何以我想要的方式 map colors? 有沒有更簡單的方法來制作這個散點圖?
當前 python:
# Define data and labels to be used in scatter plot
plotlabels = np.sort(df_movie['ratingscat'].unique())
plotdata = [df_movie[df_movie['ratingscat']==label][['likes_winsor','gross']] for label in plotlabels]
# Define blue and red colourmap ("brg") and assign to plotlabels
colors = plt.cm.brg(np.linspace(0, 1, len(plotlabels)))
# Define chart size and create scatter plot
plt.figure(figsize=(12, 8))
for i, data in enumerate(plotdata):
plt.scatter(data['likes_winsor'],data['gross'],color=colors[i],label="Ratings: {}".format(plotlabels[i]))
# Create labels for axis and title
plt.xlabel('Movie Likes')
plt.ylabel('Movie Grosses')
plt.title('Movie ratings plotted by Gross and number of Likes')
# Add legend and grid, print scatterplot
plt.legend()
plt.grid(zorder=0)
plt.show()
嘗試簡化/對顏色進行更多控制(不起作用):
x = [df_movie[df_movie['likes_winsor']]]
y1 = [df_movie[df_movie['ratingscat']=='negative'][['gross']]
plt.scatter(x, y1, color = 'red')
y2 = [df_movie[df_movie['ratingscat']=='neutral'][['gross']]
plt.scatter(x, y2, color = 'blue')
y3 = [df_movie[df_movie['ratingscat']=='positive'][['gross']]
plt.scatter(x, y3, color = 'green')
plt.show()
任何建議/支持將不勝感激。 謝謝!
作為獎勵,如果有人知道如何讓 '1e8' 看起來更有吸引力(例如,在 y 軸上顯示 '600m' 以顯示刻度線 6*1e8),請聯系我:)
在這 3 行中有一個不需要的左括號[
:
y1 = [df_movie[df_movie['ratingscat']=='negative'][['gross']]
y2 = [df_movie[df_movie['ratingscat']=='neutral'][['gross']]
y3 = [df_movie[df_movie['ratingscat']=='positive'][['gross']]
應該是這樣的:
y1 = df_movie[df_movie['ratingscat']=='negative'][['gross']]
y2 = df_movie[df_movie['ratingscat']=='neutral'][['gross']]
y3 = df_movie[df_movie['ratingscat']=='positive'][['gross']]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.