从 matplotlib 导入地理图到 plotly

Posted

技术标签:

【中文标题】从 matplotlib 导入地理图到 plotly【英文标题】:Import geographic plots from matplotlib to plotly 【发布时间】:2015-01-27 07:49:35 【问题描述】:
import mpl_toolkits
import mpl_toolkits.basemap

#
# specify the map boundaries and projection type
#
mymap = mpl_toolkits.basemap.Basemap(llcrnrlon= -120, llcrnrlat=22,
                                     urcrnrlon=-58, urcrnrlat=48,
                                     projection="tmerc", lon_0 = -95, lat_0 = 35,
                                     resolution = "l")
fig_map = plt.figure(6, figsize=(10, 8))

mymap.fillcontinents(color = "lightgray")
mymap.drawcoastlines(color = "gray", linewidth=1.2)
mymap.drawcountries(color = "gray", linewidth=2)
mymap.drawstates(color = "gray")

mymap.drawmapboundary()

plt.show()

matplotlib 给了我这个美丽的情节:

现在,我想将其导入到情节交互情节中

py.iplot_mpl(fig_map, filename='DataScience/data-visualization/geographic_map_plot_1')

/usr/local/lib/python2.7/dist-packages/plotly/matplotlylib/renderer.py:479: UserWarning:

I found a path object that I don't think is part of a bar chart. Ignoring.

我收到如下几个奇怪的错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-79-561cd71f8586> in <module>()
----> 1 py.iplot_mpl(fig_map, filename='DataScience/data-visualization/geographic_map_plot_1')

/usr/local/lib/python2.7/dist-packages/plotly/plotly/plotly.pyc in iplot_mpl(fig, resize, strip_style, update, **plot_options)
    257             "object. Run 'help(plotly.graph_objs.Figure)' for more info."
    258         )
--> 259     return iplot(fig, **plot_options)
    260 
    261 

/usr/local/lib/python2.7/dist-packages/plotly/plotly/plotly.pyc in iplot(figure_or_data, **plot_options)
    113     if 'auto_open' not in plot_options:
    114         plot_options['auto_open'] = False
--> 115     res = plot(figure_or_data, **plot_options)
    116     urlsplit = res.split('/')
    117     username, plot_id = urlsplit[-2][1:], urlsplit[-1]  # TODO: HACKY!

/usr/local/lib/python2.7/dist-packages/plotly/plotly/plotly.pyc in plot(figure_or_data, validate, **plot_options)
    185                                          "seeing this error:\n\n0"
    186                                          "".format(err))
--> 187         if not figure['data']:
    188             raise exceptions.PlotlyEmptyDataError(
    189                 "Empty data list found. Make sure that you populated the "

KeyError: 'data'

我还没有在地图上看到任何关于 plotly 的例子!因此,非常感谢任何帮助!

【问题讨论】:

【参考方案1】:

很遗憾,Plotly 还不支持底图转换。

也就是说,有一个解决方法:

http://nbviewer.ipython.org/github/etpinard/plotly-misc-nbs/blob/etienne/plotly-maps.ipynb

简而言之,您需要从底图对象中提取 x-y 坐标,将它们附加到 Plotly 跟踪对象并调用 plotly.plotly.plot()

如果您对 Plotly 有任何其他问题,请随时与我联系。

【讨论】:

谢谢,参加聚会有点晚了,不知道这个功能是否已添加?

以上是关于从 matplotlib 导入地理图到 plotly的主要内容,如果未能解决你的问题,请参考以下文章

matplot:在matplotlib中显示标签的问题[重复]

ImportError:无法从“matplotlib.mlab”导入名称“PCA”

导入错误:无法从部分初始化的模块“matplotlib”导入名称“ft2font”

使用 matplotlib.basemap 制作的地理图是不是受版权保护?

将直方图从 matplotlib 导入到 plotly

在 python 和 matplotlib 中带有线条的地理数据图/地图