PNG 图像未显示在 Folium 地图上
PNG image not being displayed on Folium map
我 运行 在 here.
的 folium 地图上显示图像的这个 folium 示例
我 运行 这个程序在我的 spyder 上,尽管它在 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')
预期结果
我的输出
使用folium-0.10.1
,您只需修改这一行:
encoded = base64.b64encode(open(png, 'rb').read())
进入
encoded = base64.b64encode(open(png, 'rb').read()).decode()
你得到:
我 运行 在 here.
的 folium 地图上显示图像的这个 folium 示例我 运行 这个程序在我的 spyder 上,尽管它在 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')
预期结果
我的输出
使用folium-0.10.1
,您只需修改这一行:
encoded = base64.b64encode(open(png, 'rb').read())
进入
encoded = base64.b64encode(open(png, 'rb').read()).decode()
你得到: