PNG image not being displayed on Folium map

荒凉一梦 提交于 2020-03-05 04:27:06

问题


I ran this folium example of image display on folium map from here.

I ran this program on my spyder though it works fine on jupyter

import numpy as np
import pandas as pd
import numpy.ma as ma
import folium

def make_data():
    x = np.linspace(-np.pi, np.pi, 101)
    sin = np.sin(x)
    cos = np.cos(x)
    cos[20:50] = np.NaN
    return pd.DataFrame(np.asanyarray([sin, cos]).T, columns=['sin', 'cos'], index=x)

df = make_data()
resolution, width, height = 75, 7, 3

station = '42'
lon, lat = -42, -21
m = folium.Map(location=[lat, lon], zoom_start=5)

import base64
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(width, height))
ax = df.plot(ax=ax, legend=False)
ax.set_ylabel('Sea surface height (m)')
png = 'mpld3_{}.png'.format(station)
fig.savefig(png, dpi=resolution)

encoded = base64.b64encode(open(png, 'rb').read())
from folium import IFrame

html = '<img src="data:image/png;base64,{}">'.format
iframe = IFrame(html(encoded), width=(width*resolution)+20, height=(height*resolution)+20)
popup = folium.Popup(iframe, max_width=2650)

icon = folium.Icon(color="red", icon="ok")
marker = folium.Marker(location=[lat-2, lon-1], popup=popup, icon=icon)
marker.add_to(m)
m.save('test.html')

expected result

My output


回答1:


Using folium-0.10.1, you simply have to modify this line:

encoded = base64.b64encode(open(png, 'rb').read())

into

encoded = base64.b64encode(open(png, 'rb').read()).decode()

and you get:



来源:https://stackoverflow.com/questions/58227034/png-image-not-being-displayed-on-folium-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!