0

For me, the most convenient way to instantiate a figure with a regular grid of subplots is matplotlib.pyplot.subplots but I cannot specify which type of projection is desired for the subplots

In [45]: from matplotlib.pyplot import subplots 
    ...: from mpl_toolkits.mplot3d import axes3d 
    ...: fig, axes = subplots(2,2, projection='3d')                                       
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-45-023ffffb6940> in <module>
      1 from matplotlib.pyplot import subplots
      2 from mpl_toolkits.mplot3d import axes3d
----> 3 fig, axes = subplots(2,2, projection='3d')

~/lib/miniconda3/lib/python3.7/site-packages/matplotlib/pyplot.py in subplots(nrows, ncols, sharex, sharey, squeeze, subplot_kw, gridspec_kw, **fig_kw)
   1207 
   1208     """
-> 1209     fig = figure(**fig_kw)
   1210     axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,
   1211                        squeeze=squeeze, subplot_kw=subplot_kw,

~/lib/miniconda3/lib/python3.7/site-packages/matplotlib/pyplot.py in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, clear, **kwargs)
    543                                         frameon=frameon,
    544                                         FigureClass=FigureClass,
--> 545                                         **kwargs)
    546 
    547         if figLabel:

~/lib/miniconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py in new_figure_manager(cls, num, *args, **kwargs)
   3258         from matplotlib.figure import Figure
   3259         fig_cls = kwargs.pop('FigureClass', Figure)
-> 3260         fig = fig_cls(*args, **kwargs)
   3261         return cls.new_figure_manager_given_figure(num, fig)
   3262 

TypeError: __init__() got an unexpected keyword argument 'projection'

and I don't know if it is possible to inform an Axes, as returned from subplots, that I want to use it for a 3D plotting, e.g.,

In [46]: fig, ax = subplots()                                                             

In [47]: ax.set_p[TAB]
                  set_path_effects() set_position()    
                  set_picker()       set_prop_cycle()     

How can I change the projection type for a pre-existing Axes object?

gboffi
  • 22,939
  • 8
  • 54
  • 85
  • You cannot "inform" a 2D axes to become a 3D axes all of a sudden (similar to how you cannot inform your bicycle to become a Harley-Davidson). However, setting the projection at instantiation is easily possible with `plt.subplots(subplot_kw={"projection" : "3d"})` – ImportanceOfBeingErnest Feb 21 '20 at 14:00
  • @ImportanceOfBeingErnest Thank you for the link! somehow neither google nor SO were able to find this specific Q&A – gboffi Feb 21 '20 at 15:04
  • Agreed. With Stackoverflow slowly mutating to a homework platform it's getting more and more difficult to find the useful Q&As. – ImportanceOfBeingErnest Feb 21 '20 at 15:39
  • I leave open this question as its title seems to be more relevant for a casual search on the subject matter. – gboffi Feb 29 '20 at 19:35

0 Answers0