Adding config modes to Plotly.Py offline - modebar - python

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.

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)

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']))

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.

Related

Interactive Xarray dataset raster visualisation app using Panel and hvplot

I am trying to replicate the Glaciers Demo using an Xarray of geospatial data. I am able to create pretty much exactly what I want but I am trying to create a Panel app that allows the user to select the data_vars, each of which has different dimensions that I want make interactable, and visualize on an interactive map with at least the continents contour. Here is what my Xarray Dataset looks like :
def plot(field):
return xds[field].hvplot.image().opts(cmap='jet',height=650,width=1300,data_aspect=1)
interact(plot, field = list(xds.data_vars))
and here is what the code above produces in a notebook :
I would like to integrate the selector for the data_vars and then depending on its dimensions have interactive maps with controls for all its dimensions (ES has (time, pres1, lat, lon) while P0 has only (time, lat, lon)) and I would like to have the controls in the sidebar and the plots in the main of the following template :
from turtle import width
from matplotlib.pyplot import title
import panel as pn
import numpy as np
import holoviews as hv
from panel.template import DefaultTheme
from pathlib import Path
import fstd2nc
import hvplot.xarray
import xarray as xr
from unicodedata import name
import hvplot
import param
from panel.interact import interact
pn.extension(sizing_mode='stretch_width')
bootstrap = pn.template.MaterialTemplate(title='Material Template', theme=DefaultTheme, )
glob_path = Path(r"C:\Users\spart\Documents\Anaconda-Work-Dir")
file_list = [str(pp).split('\\')[-1] for pp in glob_path.glob("2022*")]
phase = pn.widgets.FloatSlider(name="Phase", start=0, end=np.pi)
fileSel = pn.widgets.Select(name='Select File', options=file_list)
#pn.depends(fileSel=fileSel)
def selectedFile(fileSel):
base_path = r"C:\Users\spart\Documents\Anaconda-Work-Dir\{}".format(fileSel)
return pn.widgets.StaticText(name='Selected', value=base_path)
#pn.depends(fileSel=fileSel)
def dataXArray(fileSel):
base_path = r"C:\Users\spart\Documents\Anaconda-Work-Dir\{}".format(fileSel)
xds = fstd2nc.Buffer(base_path).to_xarray()
return xds.ES.hvplot( width=500)
bootstrap.sidebar.append(fileSel)
bootstrap.sidebar.append(selectedFile)
bootstrap.main.append(
pn.Row(
pn.Card(hv.DynamicMap(dataXArray), title='Plot'),
)
)
bootstrap.show()
EDIT : Here is a link to an example dataset which can be loaded with the following code
xds = fstd2nc.Buffer(PATH_TO_FILE).to_xarray()
Without the data file I can't easily run the code, but some observations:
If using bare functions like this rather than classes, I'd recommend using pn.bind rather than pn.depends; it really helps get the code organized better.
For a simple application like this, I'd use hvPlot .interactive: https://hvplot.holoviz.org/user_guide/Interactive.html
I can't seem to find this in the docs, but you can pull out the widgets from the result of dataXArray (or any other hvplot or holoviews object) using .widgets(), and you can then put that in the sidebar. You can then pull out just the plot using .panel(), and put that in the main area.
If that helps you get it done, then great; if not please post a sample data file or two so that it's runnable, and I can look into it further. And please submit a PR to the docs once you get it working so that future users have less trouble!

Multiple plotly plots on 1 page without subplot

I want to have multiple plotly plots on 1 html page without using the tools.make_subplots method. (I dont want to use that since I find that its not easy to read and I want a unique legend & layout in each of the subplot panels).
I want to define 2 figures with their own unique layouts, and arrange them arbitrarily on the page. I think I know how to do this with dash using the html.Div object, but I was wondering if there was an easy way to do this using only plotly?
I encountered the very same problem and followed the solution posted here:
Plotly: Plot multiple figures as subplots by Esostack
However, when I dumped the html of multiple figures into a single text file, I found the file size increasing by 5MB per figure that I add. 99.9% of this is caused by java script stuff that is added by plotly to make the plots interactive. Luckily, they also implemented a parameter to specify if you want to include the js or not. So you need to include it only for the first figure and skip it for the rest, like it is done in the following function. Hope that helps:
def figures_to_html(figs, filename):
'''Saves a list of plotly figures in an html file.
Parameters
----------
figs : list[plotly.graph_objects.Figure]
List of plotly figures to be saved.
filename : str
File name to save in.
'''
import plotly.offline as pyo
dashboard = open(filename, 'w')
dashboard.write("<html><head></head><body>" + "\n")
add_js = True
for fig in figs:
inner_html = pyo.plot(
fig, include_plotlyjs=add_js, output_type='div'
)
dashboard.write(inner_html)
add_js = False
dashboard.write("</body></html>" + "\n")
So, in conclusion, I have not found a way to do this purely with plotly. Below is my code for doing this with Dash, which has been working quite well:
Step 1: make a few plotly plots
import plotly.offline as pyo
import plotly.graph_objs as go
import plotly as py
fig1 = go.Scatter(y=[1,2,3])
fig2 = go.Scatter(y=[3,2,1])
plots = [fig1, fig2]
Step 2: Make dash Div objects:
app = dash.Dash()
layout = html.Div(
[html.Div(plots[i]) for i in range(len(plots))],
style = {'margin-right': '0px'}
)
Step 3: Run dash
app.layout = layout
app.run_server(port=8052)

Cast Plotly object to HTML and edit modeBar before plotting in IPython Notebook

I'm trying to edit the modeBarButtons of a plotly graph such that the 'save and edit plot in cloud' function becomes disabled. At first this didn't seem possible in Python. But this post pointed out a workaround for Python, by casting the object to HTML and editing the HTML code.
Unfortunately, the 'save and edit plot in cloud' functionality remains when I try the example code below:
import numpy as np
from IPython.display import display, HTML
from plotly.offline import download_plotlyjs, init_notebook_mode, plot
import plotly
import plotly.graph_objs as go
plotly.offline.init_notebook_mode()
# create sin data to plot
x = np.arange(0.0, 100.0, 0.1)
y = np.sin(x)
# create plot data, edit and plot
trace0 = go.Scatter(x=x,y=y, name="Sin")
HTML_code = plot([trace0], show_link=False, auto_open=False, output_type='div')
HTML_code = HTML_code.replace('modeBarButtonsToRemove:[]',
'modeBarButtonsToRemove:["sendDataToCloud"]')
display(HTML(HTML_code))
[Edit] Write to file:
The button is successfully removed when the edited HTML is written to file and opened in a browser.
Reloading the edited file into my notebook was unsuccessful:
from IPython.display import display, HTML
with open('./temp-plot.html', 'r') as file :
tempHTML = file.read()
display(HTML(tempHTML))
Javascript error adding output!
ReferenceError: Plotly is not defined
See your browser Javascript console for more details.
Using output_type='div' instead of output_type='file' (my preferred approach due to write restrictions for the final product):
The notebook seems to 'reload' plotlys' standard configuration and only insert the data from the HTML, leaving the edited JavaScript unused.
Any suggestions?
Regards,
Koen

How to prevent plotly from plotting automatically

I just discovered plotly and like it so far. I have this code provided by the main website
import plotly.plotly as py
from plotly.graph_objs import *
trace0 = Scatter(
x=[1,2,3,4],
y=[10,15,13,17]
)
trace1 = Scatter(
x=[1,2,3,4],
y=[16,5,11,9]
)
data = Data([trace0, trace1])
unique_url = py.plot(data, filename='basic-line')
I am curious about two things:
1) When I run this code, my browser automatically pops up and shows me the graph. All I want is the url so that I can later embed it in an html file. Is there a way to turn off the feature that opens my browser and shows me the graph?
2) Is there a way to get rid of the 'Play with this data' link?
I have combed through the documentation provided, but have come up empty-handed on these two issues.
To disable pop-ups you could use auto_open=FALSE and try the following
py.plot(data, filename='basic_line', auto_open=False)
py.plot(data, show_link=False) will take that link off (if you are referring to the link that says Export to plot.ly). At least it does using:
import plotly.offline as py. As for the link at the top (when you hover your mouse over the graph), I'm trying to get rid of the Save and edit plot in cloud but only find options for that under the java script version... and that hides the whole bar which has other useful items on it (javascript option is: {displayModeBar: false}). Obviously I am finding the reference to "play with this data" ambiguous. You can see the workaround I wrote here: Adding config modes to Plotly.Py offline - modebar
You can easily remove that Export to plot.ly link in the offline graph.
Open your saved html file in a text editor. and search for. {"showLink": true, "linkText": "Export to plot.ly"}
And change the true value to false.

Python SAC plot w/ grid

I'm required to use the information from a .sac file and plot it against a grid. I know that using various ObsPy functions one is able to plot the Seismograms using st.plot() but I can't seem to get it against a grid. I've also tried following the example given here "How do I draw a grid onto a plot in Python?" but have trouble when trying to configure my x axis to use UTCDatetime. I'm new to python and programming of this sort so any advice / help would be greatly appreciated.
Various resources used:
"http://docs.obspy.org/tutorial/code_snippets/reading_seismograms.html"
"http://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.plot.html#obspy.core.stream.Stream.plot"
The Stream's plot() method actually automatically generates a grid, e.g. if you take the default example and plot it via:
from obspy.core import read
st = read() # without filename an example file is loaded
tr = st[0] # we will use only the first channel
tr.plot()
You may want to play with the number_of_ticks, tick_format and tick_rotationparameters as pointed out in http://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.plot.html.
However if you want more control you can pass a matplotlib figure as input parameter to the plot() method:
from obspy.core import read
import matplotlib.pyplot as plt
fig = plt.figure()
st = read('/path/to/file.sac')
st.plot(fig=fig)
# at this point do whatever you want with your figure, e.g.
fig.gca().set_axis_off()
# finally display your figure
fig.show()
Hope it helps.

Categories