我的數(shù)據(jù)如下所示: time distance color1 2017-10-10 14:04:14 0.006 yellow2 2017-10-10 14:04:15 0.011 green3 2017-10-10 14:04:46 0.051 green4 2017-10-10 14:04:56 0.063 red5 2017-10-10 14:05:06 0.073 red6 2017-10-10 14:05:16 0.081 green7 2017-10-10 14:05:26 0.095 green8 2017-10-10 14:05:36 0.103 green9 2017-10-10 14:05:46 0.113 green10 2017-10-10 14:05:56 0.124 green11 2017-10-10 14:06:06 0.134 green12 2017-10-10 14:06:16 0.149 yellow13 2017-10-10 14:06:26 0.158 yellow我的代碼是這樣的:fig, ax1 = plt.subplots(figsize=(30,10))color = 'tab:red'ax1.plot_date(df['time'], df['distance'], marker='o',color='red') ax1.set_xlabel('Time', fontsize=20)ax1.set_ylabel('distance', color=color, fontsize=20) ax1.set_ylim(bottom=0,top=80)ax1.set_xlim(left=xmin, right=xmax) # I set the boundary for x-axis我想根據(jù)列為每個點分配不同的顏色df['color']。如果我將代碼更改為,則會出錯。ax1.plot_date(df['time'], df['distance'], marker='o',color=df['color']) 錯誤:ValueError: Invalid RGBA argument: 0 yellow1 yellow2 green3 green4 red5 red6 green如果有人知道如何使用第三列為不同類別標(biāo)簽設(shè)置顏色,我將不勝感激plt.plot_date()。注意:我使用plt.plot_date()而不是plt.scatter()因為它允許我選擇在特定圖形上顯示的時間范圍并更輕松地設(shè)置 timeTickers。
1 回答

繁星淼淼
TA貢獻(xiàn)1775條經(jīng)驗 獲得超11個贊
您可以groupby為每種顏色分別繪制它們:
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots(figsize=(30,10))
color = 'tab:red'
for pcolor, gp in df.groupby('color'):
ax1.plot_date(gp['time'], gp['distance'], marker='o', color=pcolor)
...
添加回答
舉報
0/150
提交
取消