ItGo.me Focus on IT Recommend

Home > python - Import geographic plots from matplotlib to plotly

python - Import geographic plots from matplotlib to plotly

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

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

And matplotlib gave me this beautiful plot:

Now, I want to import this to plotly intyerative plot

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.

And I receive several weird errors as below:

---------------------------------------------------------------------------
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\n{0}"
    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'

I haven't seen any example for plotly on geographic maps! So, any help is much appreciated!

python matplotlib plot plotly
|
  this question
asked Nov 28 '14 at 15:58 Vahid Mir 1,563 8 24 44

 | 

1 Answers
1

Unfortunately, Plotly does not support basemap conversions yet.

That said, there is a work-around:

Recommend:python - Bar charts with xaxis bins using Plotly or Matplotlib

to read. What I want to do is create bins on the x axis. For example create 1 bar of the total y values for x values from 0-50. And 50-100. And so on. Can this be done using matplotlib or Plotly Plotly Code: data = Data([ Bar(

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

In brief, you need to extract the x-y coordinates from the basemap object, attached them to a Plotly trace objects and call plotly.plotly.plot().

If you have any other questions about Plotly, feel free to contact me.


|
  this answer
answered Nov 28 '14 at 16:49 etpinard 1,344 2 11

 | 

Recommend:python - Plot 3D plots using plotly

/ the ID of the div, created above graphs.data, graphs.layout || {});</script> server side @app.route('/', methods=['POST'])def my_form_post():x, y, z = np.random.multivariate_normal(np.array([0

------splitte line----------------------------