PNG图片未显示在Folium地图上

时间:2019-10-03 21:20:02

标签: python folium

我运行了https://nbviewer.jupyter.org/gist/ocefpaf/20aa2e74e11db30da2ff07c45cd74816的大叶地图上的该大叶示例图像显示

我在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')

预期结果

enter image description here

我的输出

enter image description here

1 个答案:

答案 0 :(得分:2)

使用folium-0.10.1,您只需修改以下行:

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

进入

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

您会得到:

enter image description here