Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding config modes to Plotly.Py offline - modebar

Tags:

python

plotly

Plotly.js includes all the parameters needed to configure the ModeBar, which allows one to take away options from the display bar (such as the link to edit the graph online). However, this does not appear implemented in the Plotly.py API. In the js version:

Plotly.newPlot('myDiv', data, layout, {displayModeBar: false}); Removes the modebar entirely.
Plotly.newPlot('myDiv', data, layout, {displaylogo: false}, {modeBarButtonsToRemove: ['sendDataToCloud','hoverCompareCartesian']}) allows one to specify each button to remove which I'd like to implement.

I've edited this as I've found a workaround... see the answer I posted below. Can come in handy for those that have other parameters that they would like to use.

like image 804
Matt Avatar asked Dec 07 '22 22:12

Matt


1 Answers

Open the HTML file, search for modeBarButtonsToRemove:[] then replace with the buttons you want removed, for my purpose modeBarButtonsToRemove:['sendDataToCloud']

To remove the Plotly Logo and link, search for displaylogo:!0 and replace with displaylogo:!1

Here is a demo using Python:

from plotly.offline import plot
import plotly.graph_objs as go
import webbrowser
import numpy as np
import pandas as pd

# generate your Plotly graph here

N = 500
y = np.linspace(0, 1, N)
x = np.random.randn(N)
df = pd.DataFrame({'x': x, 'y': y})
data = [go.Histogram(x=df['x'])]

# plot it for offline editing
HTMLlink = plot(data, show_link=False, auto_open=False)[7:] #remove the junk characters
# now need to open the HTML file
with open(HTMLlink, 'r') as file :
  tempHTML = file.read()
# Replace the target strings
tempHTML = tempHTML.replace('displaylogo:!0', 'displaylogo:!1')
tempHTML = tempHTML.replace('modeBarButtonsToRemove:[]', 'modeBarButtonsToRemove:["sendDataToCloud"]')
with open(HTMLlink, 'w') as file:
  file.write(tempHTML)
del tempHTML

webbrowser.open(HTMLlink)
like image 144
Matt Avatar answered Dec 10 '22 10:12

Matt