Issue
I'm having 2 problems with implementing folium in a pyqt5 UI:
The legend in my choropleth is showing the correct numbers, but the color in the legend is not displaying.
The code snippet I have listed below is not showing tooltips.
The .json and data can be found here:
https://github.com/python-visualization/folium/blob/master/examples/data/us-states.json
https://github.com/python-visualization/folium/blob/master/examples/data/US_Unemployment_Oct2012.csv
The code to both questions is here:
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QHBoxLayout, QVBoxLayout
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
import folium
import io
import pandas as pd
import os
class foliumWidget(QWidget):
def __init__(self, parent = None):
#super().__init__()
QWidget.__init__(self, parent)
layout = QVBoxLayout()
self.setLayout(layout)
state_data = 'US_Unemployment_Oct2012.csv'
us_geo = 'us-states.json'
df = pd.read_csv(state_data, na_values=[" "])
m = folium.Map(location=[48, -102], tiles="cartodbpositron", zoom_start=3)
choropleth = folium.Choropleth(
geo_data= us_geo,
name="choropleth",
data=df,
columns=["State","Unemployment"],
key_on="feature.id",
fill_color="PuRd",
fill_opacity=0.7,
line_opacity=0.1,
legend_name="Unemployment",
reset=True,
).add_to(m)
choropleth.geojson.add_child(
folium.features.GeoJsonTooltip(['State'])
)
#folium.LayerControl().add_to(m)
data = io.BytesIO()
m.save(data, close_file=False)
webView = QWebEngineView()
webView.setHtml(data.getvalue().decode())
layout.addWidget(webView)
Solution
An error in the code provided by the OP is that GeoJsonTooltip expects the fields of the geojson as fields, not the .csv so it throws an exception.
Eliminating redundant code you get:
import io
import os
from pathlib import Path
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineView
import folium
import pandas as pd
class FoliumWidget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
layout = QVBoxLayout(self)
current_dir = Path(__file__).resolve().parent
state_data = "US_Unemployment_Oct2012.csv"
us_geo = "us-states.json"
df = pd.read_csv(str(current_dir.joinpath(state_data)), na_values=[" "])
m = folium.Map(location=[48, -102], tiles="cartodbpositron", zoom_start=3)
choropleth = folium.Choropleth(
geo_data=str(current_dir.joinpath(us_geo)),
name="choropleth",
data=df,
columns=["State", "Unemployment"],
key_on="feature.id",
fill_color="PuRd",
fill_opacity=0.7,
line_opacity=0.1,
legend_name="Unemployment",
reset=True,
).add_to(m)
choropleth.geojson.add_child(folium.features.GeoJsonTooltip(["name"]))
webView = QWebEngineView()
layout.addWidget(webView)
data = io.BytesIO()
m.save(data, close_file=False)
webView.setHtml(data.getvalue().decode())
def main():
import sys
app = QApplication(sys.argv)
widget = FoliumWidget()
widget.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.