Plotting python DataFrame result? Send back to a query result table to make a standard chart?

Hi there,

I needed to do some data analysis (calculating quantiles) that required going into the python notebook section. Now I need to figure out how I can plot this data. Matplotlib plots look terrible. It would be *really* great if I could just use the same engine that generates charts from the SQL queries.

The output dataframe looks something like this:

                  q95        q97        q99       q98
date
2017-08-08  47.857639  67.332755  93.483218  82.19213
2017-08-09  40.043981  64.570370  90.484259  77.42963

Although with many more rows. The columns are the 95%, 97%, etc quantiles for each day.

Is there any way to send the contents of a DF into a table as-if it's a query result, and then generate a plot with it?

Or is there a way to generate the same sort of plot I would get from the standard line chart, and easily drop it into the report?

5replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Hi Andrew , We've had requests for Python to be able to use Mode's chart UI so it is being considered.  I can add your request for this feature so the product team will see it.   Perhaps using plotly for charting might be a better option?  Can you tell me what kind of chart you wanted to create and I'll see if we have an example I can share.  Thanks!

    Reply Like
  • Line chart, with the columns (quantiles) over time. Overlayed area plot would work, too, but that would take reverse ordering of the columns.

    I ended up using plotly, after doing enough searching to find a way to get done what I wanted:

    from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
    from plotly.graph_objs import *
    init_notebook_mode()
    
    data = [{'x': df_loss.index.tolist(), 'y': df_loss[col], 'name': col}  for col in df_loss.columns]
    
    layout = Layout(
        #title='Data Loss Quantiles by day',
        #width= 1000,
        margin=dict(b=100,l=50,r=0,t=0),
        height= 480,
        xaxis=dict(
          tickangle=-45,
          title='Date'
        ),
        yaxis=dict(
            title='Data Loss [%]',
            range=[0, 100]
        ),
    )
    
    fig = dict(data=data, layout=layout)
    iplot(fig)

    That does work, and it gives me a "nice" interactive plot (so I can see the values with a mouseover), but playing around with the margins and height took some work.

    And sometimes the height isn't set correctly when the report runs, especially after the form parameters section is collapsed.

    Reply Like
  • You can also adjust Python chart heights using CSS, in case that helps:

    <script>
    
      $("mode-python .chart-content").css("height",1000)
      $("mode-python .mode-python").css("height",1000)
      $("mode-python .mode-python").css("max-height",1000)
      $("mode-python img").css("max-height",1000)
    
    </script>
    Reply Like
  • I would also like the ability to plot a Pandas DataFrame.

    It would be immensely helpful. Can we just use the Chart Studio and select a source from there?

    Reply Like
  • Hey Samarth, unfortunately we don't offer that ability at this time. We've received this request from other customers, and I've gone ahead and added your voice to that request. I'll be sure someone reaches out to you if we decide to move forward with that!

    In the meantime, you can use a library such as plotly or matplotlib to achieve visualizations in your Mode Python notebook. See Available Libraries for the full list!

    Reply Like
Like Follow
  • Status Answered
  • 1 mth agoLast active
  • 5Replies
  • 599Views
  • 5 Following

Create A New Discussion

Share your thoughts