简体   繁体   English

在 pyqt5 中实现的 folium choropleth 图例中没有颜色,并且工具提示未显示

[英]No color in folium choropleth legend implemented in pyqt5 and tooltips not showing

I'm having 2 problems with implementing folium in a pyqt5 UI:我在 pyqt5 UI 中实现 folium 时遇到 2 个问题:

  1. The legend in my choropleth is showing the correct numbers, but the color in the legend is not displaying.我的 choropleth 中的图例显示正确的数字,但图例中的颜色未显示。

  2. The code snippet I have listed below is not showing tooltips.我在下面列出的代码片段没有显示工具提示。

The.json and data can be found here: .json 和数据可以在这里找到:

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)

在此处输入图像描述

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. OP 提供的代码中的一个错误是 GeoJsonTooltip 期望 geojson 的字段作为字段,而不是 .csv 因此它会引发异常。

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()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM