Unable to insert altair visualisation as popup in folium map - python

I'm relatively new to python and this is the first project that I'm working on. I'm trying to plot a simple multi line chart using altair onto a folium marker's popup. I followed the examples on this page https://github.com/python-visualization/folium/blob/master/examples/Popups.ipynb. But when I click on the marker, it's just plain white. The map and markers are plotted fine except the popup.
This is what my whole dataset looks like :
Here is the code:
import pandas as pd
import folium
import altair as alt
import json
chart = alt.Chart(df).mark_line().encode(
x='index',
y='Ambala',
color='Variable')
chart_2 = json.loads(chart.to_json())
m = folium.Map([30,-80],zoom_start=2)
popup = folium.Popup(max_width=650)
folium.Vega(chart_2, height=350, width=650).add_to(popup)
folium.Marker([30, -80], popup=popup).add_to(m)
m
Note that this just a sample of code. I'll be implementing this on a larger scale in my project.
I tried to plot a visualization (which I found here : https://github.com/python-visualization/folium/blob/master/examples/data/vis1.json) and this seemed to work fine. I just don't understand why the same code works for their plot but not for mine even though altair produces a fine looking chart from my code. Sorry if this is sounds silly, I'm still a beginner and python is my first language. Thanks

If you want to use an Altair chart inside a folium map, you need to use a VegaLite object instead of a Vega object. You just need to change the line with the Vega object like that : folium.features.VegaLite(chart_2, height=350, width=650).add_to(popup)

Related

Update plotly chart in jupyter notebook

I'm creating a plotly chart in jupyter notebook. Because I'm testing some algorithm I want to add data after the initial fig2.show(). But when I update the data and call fig2.show again a new chart is being rendered. How can I update the chart instead of creating a new chart?
This should be an easy task - but it's way to hard to find for me in the documentation.
import plotly.offline as pyo
import plotly.graph_objs as go
import numpy as np
# Set notebook mode to work in offline
pyo.init_notebook_mode()
fig2 = go.Figure(data=go.Scatter(x=moreX2, y=moreY2))
fig2.show()
Then I'm using update:
moreX2.append(2)
moreY2.append(5)
fig2.data[0].update({"x": moreX2, "y": moreY2})
The chart isn't being rerendered and I tried to call fig.show() again, which just creates a new chart.
This answer worked for me, and seems like the ideal answer to this question as well. I've voted to mark this as a duplicate.
To summarize: by wrapping the Figure in a FigureWidget, we can now do exactly what the OP wanted. Although on Colab I did have to give some extra permission by calling the following:
from google.colab import output
output.enable_custom_widget_manager()

How to save a Plotly graph in a ipynb file?

I understand that it is possible to export a Plotly graph, and that I can display it.
While sharing notebooks, the matplotlib plots remain intact in the Jupyter Notebooks, however, the Plotly graphs do not. They simply disappear
I understand that the Plotly graph is browser rendered, but is there any way I can store the graph in the ipynb file when I export it?
Is there any way that I can display the Plotly graph, just like the matplotlib graph?
Edit: As suggested in an answer, I tried to save it to a figure object, and display that, but no luck there either :/
If you put the code for a series of graphs in a single cell, execute it and save it, I think it will be displayed the next time you open it.
import plotly.express as px
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()

Writing Data Point Name Just Above the Data Point in Python Plotly

I have a dataset and I plotted using plotly python
The name of the models is shown as a legend on the right side which makes it a little difficult to interpret. Is there any way to write the model name just above the color circle represent on the graph similar to what shown below? Something similar in mathplotlib will also help me.
You need to add labels in you scatter statement.
import plotly.express as px
#df data
fig = px.scatter(df, x="latency", y="AP", text="type", size_max=60)
fig.update_traces(textposition='top center')

Excessing Maps in python matplotlib graps

I am making a Tkinter based application in python which tracks the movement of users and plot them on a graph.
now ploting the points on a graph is no biggie, but what i couldnt manage was to give the background image as a map
currently my progress for this particular feature is nothing but as follows.
import matplotlib.pylab as plt
a=[[],[]]
a[0]=[12.28384,12,23434]#100's of values extracted from some csv file
a[1]=[80.12332,80.13312]#consider a[0],a[1] as latitude,longitude respectively
plt.plot(a[0],a[1])
plt.show()
This answer explains how to embed a map in a Tkinter window and may help.
You could also look at this example that uses the GooMPy library to embed an interactive google map in a Tkinter window.

How do you get the range of the x-axis of a Plotly graph?

I'm trying to retrieve the range of the x-axis when you zoom in a plotly scatter plot, but when I tried to access it using:
figure['layout']['xaxis']['range']
it just returns 'None'.
This is the code that I am using to create the graph:
# self.plot contains the dataframe passed to the function to be plotted
self.plot = df
# Creates the plotly plot figure
self.fig = self.plot.iplot(asFigure=True,kind='scatter', xTitle='Date', yTitle='Temperature')
# Displays the plot
iplot(self.fig, show_link=False)
I'm using cufflinks to create the plot from a pandas DataFrame so I don't explicitly set the layout.xaxis.range to anything. Also I'm using a Jupyter Notebook to display the graph, if that helps at all. So is there any way of getting the range of the x-axis of the current view window of the plot? Thanks in advance!
That does not seem to be possible at the moment using python. There's a post on community.plot.ly that says:
Graph parameters don’t dynamically change with chart actions. However,
as you zoom, the plot does emit data regarding the new x-axis range,
you’re unable to to access this information directly in Python.
Instead you’d have to use javascript:
https://plot.ly/javascript/zoom-events/

Categories