簡體   English   中英

在線圖中用標記突出顯示單個點

[英]Highlight a single point with a marker in lineplot

我想使用標記在我的線圖上突出顯示一個點。 到目前為止,我設法創建了我的 plot 並在我想要的地方插入了突出顯示。

問題是我有 4 個不同的線圖(4 個不同的分類屬性)並且我將標記放置在每個單線圖上,如下圖所示:

陰謀

我只想將標記放在 2020 線(紫色線)上。 到目前為止,這是我的代碼:

import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
import seaborn as sns
import numpy as np
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(15,10))
gs0 = gridspec.GridSpec(2,2, figure=fig, hspace=0.2)

ax1 = fig.add_subplot(gs0[0,:]) # lineplot
ax2 = fig.add_subplot(gs0[1,0]) #Used for another plot not shown here
ax3 = fig.add_subplot(gs0[1,1]) #Used for another plot not shown here

flatui = ["#636EFA", "#EF553B", "#00CC96", "#AB63FA"]

sns.lineplot(ax=ax1,x="number of weeks", y="avg streams", hue="year", data=df, palette=flatui, marker = 'o', markersize=20, fillstyle='none', markeredgewidth=1.5, markeredgecolor='black', markevery=[5])

ax1.yaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: '{:,.0f}'.format(x/1000) + 'K'))

ax1.set(title='Streams trend')
ax1.xaxis.set_major_locator(ticker.MultipleLocator(2))

我使用markevery字段在 position 中放置了一個標記 5. 有沒有辦法指定在哪一行/類別上放置我的標記?

編輯:這是我的 dataframe:

    avg streams date    year    number of weeks
0   145502.475  01-06   2017    0
1   158424.445  01-13   2017    1
2   166912.255  01-20   2017    2
3   169132.215  01-27   2017    3
4   181889.905  02-03   2017    4
... ... ... ... ...
181 760505.945  06-26   2020    25
182 713891.695  07-03   2020    26
183 700764.875  07-10   2020    27
184 753817.945  07-17   2020    28
185 717685.125  07-24   2020    29

186 rows × 4 columns

markevery是一個Line2D屬性。 sns.lineplot不返回行,因此您需要從Axes獲取要注釋的行。 從 lineplot 調用中刪除所有標記參數並添加...

lines = ax1.get_lines()

如果 2020 行/數據是系列中的第四行,

line = lines[3]
line.set_marker('o')
line.set_markersize(20)
line.set_markevery([5])
line.set_fillstyle('none')
line.set_markeredgewidth(1.5)
line.set_markeredgecolor('black')

# or
props = {'marker':'o','markersize':20, 'fillstyle':'none','markeredgewidth':1.5,
         'markeredgecolor':'black','markevery': [5]}
line.set(**props)

受 Quang Hoang 的評論啟發,另一種選擇是在從 DataFrame 導出點的點周圍/處添加一個圓圈。

x = 5    # your spec
wk = df['number of weeks']==5
yr = df['year']==2020
s = df[wk & yr]
y = s['avg streams'].to_numpy()
# or 
y = df.loc[(df['year']==2020) & (df['number of weeks']==5),'avg streams'].to_numpy()

ax1.plot(x,y, 'ko', markersize=20, fillstyle='none', markeredgewidth=1.5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM