Adding config modes to Plotly.Py offline - modebar

别等时光非礼了梦想. 提交于 2019-11-27 03:05:47

问题


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.


回答1:


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)



回答2:


This is the compact code. No need in the workaround.

plot(figure, filename='my_chart.html', show_link=False,
     config=dict(displaylogo=False,
                 modeBarButtonsToRemove=['sendDataToCloud']))



回答3:


cool workaround. There's a PR for this here: https://github.com/plotly/plotly.py/pull/410. However there was some debate as to the implementation and thus it hasn't been merged.



来源:https://stackoverflow.com/questions/36554705/adding-config-modes-to-plotly-py-offline-modebar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!