Search code examples
pythonpandasplotly

Replicate plotly plot as connected scatter plot


I want to plot a graph for one API, which has different versions in it throughout the years, with commits on the y axis.

I want to connect all the scatter plot dots together, with the version name on top of it.

This is my code as of now:

import plotly.express as px
fig = px.scatter(final_api.query("info_title=='Avi TestSeDatastoreLevel2 Object API'"), x="Year-Month", y="commits", color="info_version",title='Different Path Version found within one OAS file', width=1000, height=700)
fig.show()
fig.update_layout(yaxis_range=[0,80])

I am a bit stuck and new to plotly functions, so any guidance will be great. If there is any other library in which I could generate a similar plot, that would be helpful as well.


Solution

  • To realize your question, use the graph object to create a graph with markers, line segments, and annotations. The function required for a line graph is to create a staircase-like graph, so you set the shape of the line. Next, a color scale is applied to the markers of the scatter plot in order to color-code the markers. You can change this to whatever you need. Finally, use the annotation function to rotate the text. I have changed some of the data you have presented to make the graph look better.

    import pandas as pd
    import numpy as np
    import io
    
    data = '''
          info_version  commits Year-Month  info_title
    0       20.1.1       32    2020-08   "Avi TestSeDatastoreLevel2 Object API" 
    1       18.2.8       31    2020-01   "Avi TestSeDatastoreLevel2 Object API" 
    2       18.2.7       30    2019-12   "Avi TestSeDatastoreLevel2 Object API" 
    3       20.1.1       29    2019-11   "Avi TestSeDatastoreLevel2 Object API" 
    4       18.2.6       28    2019-10   "Avi TestSeDatastoreLevel2 Object API" 
    '''
    
    df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
    df['Year-Month'] = pd.to_datetime(df['Year-Month']) 
    
    import plotly.graph_objects as go
    
    fig = go.Figure()
    
    fig.add_trace(go.Scatter(mode='lines',
                             x=df['Year-Month'],
                             y=df['commits'],
                             line_color='gray',
                             line_width=1,
                             line_shape='vh',
                             showlegend=False
                           )
                 )
    
    fig.add_trace(go.Scatter(mode='markers',
                             x=df['Year-Month'],
                             y=df['commits'],
                             marker=dict(color=df['commits'], colorscale='Blues'),
                             showlegend=False
                            )
                 )
    
    for _,row in df.iterrows():
        fig.add_annotation(
            go.layout.Annotation(
                x=row['Year-Month'],
                y=row['commits'],
                text=row['info_version'],
                showarrow=False,
                align='center',
                yanchor='bottom',
                yshift=5,
                textangle=-90)
        )
    fig.update_layout(title_text='Different Path Version found within one OAS file',
                     template='plotly_white')
    
    fig.show()
    

    enter image description here