将配置模式添加到 Plotly.Py 离线 - 模式栏

Adding config modes to Plotly.Py offline - modebar

Plotly.js 包括配置 ModeBar 所需的所有参数,它允许从显示栏中删除选项(例如 link 以在线编辑图形)。但是,这似乎没有在 Plotly.py API 中实现。在js版本中:

Plotly.newPlot('myDiv', data, layout, {displayModeBar: false}); 完全删除模式栏。
Plotly.newPlot('myDiv', data, layout, {displaylogo: false}, {modeBarButtonsToRemove: ['sendDataToCloud','hoverCompareCartesian']}) 允许指定每个要删除的按钮。

我已经编辑了这个,因为我找到了一个解决方法...请参阅我在下面发布的答案。对于那些想使用其他参数的人来说可以派上用场。

很酷的解决方法。这里有一个 PR:https://github.com/plotly/plotly.py/pull/410。 然而,关于实施存在一些争论,因此它没有被合并。

打开 HTML 文件,搜索 modeBarButtonsToRemove:[] 然后替换为您想要删除的按钮,对于我来说 modeBarButtonsToRemove:['sendDataToCloud']

要删除 Plotly 徽标和 link,请搜索 displaylogo:!0 并替换为 displaylogo:!1

这是一个使用 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)

这是压缩代码。解决方法中不需要。

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