简体   繁体   English

在 plotly 文本注释中添加特定的选定字段

[英]Add specific selected fields in plotly text annotations

I have a graph that looks like this:我有一个看起来像这样的图表: 在此处输入图像描述

I want to color the dots in the following way, one dot for every time the version is different, like for 0.1-SNAPSHOT there are 8 dots, but I only want the first one labelled and the rest just dots (without the version),similarly for all others.我想用以下方式给点上色,每次版本不同时一个点,比如0.1-SNAPSHOT有 8 个点,但我只想要标记的第一个点和 rest 只是点(没有版本),其他人也一样。

This is how my data looks like:这是我的数据的样子:

        API_paths      info_version      Commit-growth  
24425   0             0.1-SNAPSHOT           52 
24424   20            0.1-SNAPSHOT           104    
24423   35            0.1-SNAPSHOT           156    
24422   50            0.1-SNAPSHOT           208    
24421   105           0.1-SNAPSHOT           260    
24420   119           0.1-SNAPSHOT           312    
24419   133           0.1-SNAPSHOT           364    
24576   0             0.1-SNAPSHOT           408    
24575   1             0.9.26 (BETA)          504    
24574   13            0.9.27 (BETA)          600    
24573   15            0.9.28 (BETA)          644    
24416   161           0.9.28                  28
24415   175           0.9.29                  29
24572   29            0.9.29 (BETA)          792    
24571   42            0.9.30 (BETA)          836    

Right now they are colored quite simple: fig = px.scatter(data1, x='Commit-growth', y='API_paths', color='info_version')现在它们的颜色非常简单: fig = px.scatter(data1, x='Commit-growth', y='API_paths', color='info_version')

and annotated this way:并以这种方式注释:

data1= final_api.query("info_title=='Cloudera Datalake Service'").sort_values(by='commitDate')
# data1['Year-Month'] = pd.to_datetime(final_api['Year-Month']) 
data1['Commit-growth']= data1['commits'].cumsum()

import plotly.graph_objects as go
fig = go.Figure()

fig = px.scatter(data1, x='commitDate', y='API_paths', color='info_version')
fig.add_trace(go.Scatter(mode='lines',
                         x=data1["commitDate"],
                         y=data1["API_paths"],
                         line_color='black',
                         line_width=0.6,
                         line_shape='vh',
                         showlegend=False
                       )
             )

for _,row in data1.iterrows():
    fig.add_annotation(
        go.layout.Annotation(
            x=row["commitDate"],
            y=row["API_paths"],
            text=row['info_version'],
            showarrow=False,
            align='center',
            yanchor='bottom',
            yshift=9,
            textangle=-90)
       )

fig.update_layout(template='plotly_white', title='Cloudera Datalake Service API Paths Growth',title_x=0.5,
                  xaxis_title='Number of Commit', yaxis_title='Number of Paths')
fig.update_traces(marker_size=10, marker_line_width=2, marker_line_color='black', showlegend=False, textposition='bottom center')

fig.show()

I am not sure how to achieve this, so I am a bit lost, any help will be appreciated.我不确定如何实现这一点,所以我有点迷茫,任何帮助将不胜感激。

Try creating a duplicate row of first occurrence to drive the text of your annotations.尝试创建一个重复的第一次出现的行来驱动注释的文本。

df['dupe'] = df.info_version.where(~df.info_version.duplicated(), '')

|    |   API_paths | info_version   |   Commit-growth | dupe      |
|---:|------------:|:---------------|----------------:|:----------|
|  0 |           0 | 0.1-snap       |              52 | 0.1-snap  |
|  1 |          20 | 0.1-snap       |             104 |           |
|  2 |          35 | 0.1-snap       |             156 |           |
|  3 |          50 | 0.1-snap       |             208 |           |
|  4 |         105 | 0.1-snap       |             260 |           |
|  5 |         119 | 0.1-snap       |             312 |           |
|  6 |         133 | 0.1-snap       |             364 |           |
|  7 |           0 | 0.1-snap       |             408 |           |
|  8 |           1 | 0.9-other      |             504 | 0.9-other |
|  9 |          13 | 0.9-other      |             600 |           |
| 10 |          15 | 0.9-other      |             644 |           |
| 11 |         161 | 0.9-other      |              28 |           |
| 12 |         175 | 0.9-other      |              29 |           |
| 13 |          29 | 0.9-other      |             700 |           |
| 14 |          42 | 0.9-other      |             500 |           |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM