0

I would like to start the graph from the first non-zero or non NaN value, also if possible, only connect non-zero/ non NaN terms.

def CreateAvgGraph(input_data):
    KK = test3.loc[[input_data],:]
    K = KK.T
    K = K.fillna(0)
    K = K.reset_index()
    list1a = K['index'].tolist()
    list2a = K[input_data].tolist()
    return dcc.Graph(
        id='example-graph2',
        figure={
            'data': [
                {'x' : list1a , 'y': list2a, 'type':'line','name' :input_data},
            ],
            'layout': {
                'title': str(input_data) + ' Average Price'
            }
        }
    )
    
    [![enter image description here][1]][1]

enter image description here

Removing the fillNa doesn't really help as the view scale is too much.

def CreateAvgGraph(input_data):
    KK = test3.loc[[input_data],:]
    K = KK.T
    K = K.reset_index()
    list1a = K['index'].tolist()
    list2a = K[input_data].tolist()
    return dcc.Graph(
        id='example-graph2',
        figure={
            'data': [
                {'x' : list1a , 'y': list2a, 'type':'line','name' :input_data},
            ],
            'layout': {
                'title': str(input_data) + ' Average Price'
            }
        }
    )

enter image description here

I have managed to do an ugly fix, but there has to be a better way?

def CreateAvgGraph(input_data):
    KK = test3.loc[[input_data],:]
    K = KK.T
    K = K.fillna(0)
    K = K.reset_index()
    list1a = K['index'].tolist()
    list2a = K[input_data].tolist()
    list2aa = []
    list1aa =[]
    for i in range(0,len(list1a)):
        if list2a[i] > 0:
            list1aa.append(list1a[i])
            list2aa.append(list2a[i])
        else:
            continue
return dcc.Graph(
    id='example-graph2',
    figure={
        'data': [
            {'x' : list1aa , 'y': list2aa, 'type':'line','name' :input_data},
        ],
        'layout': {
            'title': str(input_data) + ' Average Price'
  • 1
    Does this answer your question? [Plotly: How to style a plotly figure so that it doesn't display gaps for missing dates?](https://stackoverflow.com/questions/61346100/plotly-how-to-style-a-plotly-figure-so-that-it-doesnt-display-gaps-for-missing) – vestland Aug 09 '20 at 08:17

1 Answers1

1

If you simply want to plot all non-nan value, you should just drop the nan values rather than filling them with zeros, i.e. you should replace K.fillna(0) with K.dropna().

emher
  • 5,634
  • 1
  • 21
  • 32