Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Failing to properly reproduce Stata Blog's examples with Matplotlib

    Hello everyone,

    I am trying to reproduce the code given on https://blog.stata.com/2020/09/14/st...l-predictions/
    I was desperate not to find a way to use the interactive backend and eventually chose to use matplotlib.use('agg') instruction to get a graph at the end. But still, I have another problem. The following code
    Code:
    python:
    import pandas as pd
    data = pd.read_stata("predictions.dta")
    import numpy as np
    mylist = np.arange(20,90,step=10)
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    data = pd.read_stata("predictions.dta")
    ax = plt.axes(projection='3d')
    plt.savefig("essai.png")
    end
    stops at the ax creation line withe error :

    Traceback (most recent call last):
    File "/Users/alexispe/opt/anaconda3/lib/python3.7/site-packages/matplotlib/projections/__init__.py", line 58, in g
    > et_projection_class
    return projection_registry.get_projection_class(projectio n)
    File "/Users/alexispe/opt/anaconda3/lib/python3.7/site-packages/matplotlib/projections/__init__.py", line 25, in g
    > et_projection_class
    return self._all_projection_types[name]
    KeyError: '3d'

    Is there any clue to help me? I use Stata 16 with Mac OS X and an Anaconda Python setup (I have other Python versions installed if needed).
    At first, I thought that Python integration within Stata could an open window on a different (and perhaps better?) world that could threaten Stata but now I start to realise how smart the move is: it makes Stata users understand how easy, convenient and readable Stata language is!
    Last edited by Alexis Penot; 17 Mar 2021, 07:03.

  • #2
    What is returned from running:
    Code:
    python q
    python : import matplotlib; print(matplotlib.__version__)
    (the example code does not fail using python37 matplotlib 3.3.2 on updated Stata 16 on Windows)

    Comment


    • #3
      Thank you for your help. I realised from your message that my packages were not up to date. So I tried to update them from anaconda. Some weird messages appeared sometimes but at the end it seems to work since I was able to run the python code from Anaconda's Jupyter Notebook. And then came the catastrophe because now, whenever I tried to run a mere import matplotlib in Stata, Stata quit !
      So I uninstalled Anaconda and tried Python with IDLE. At that point I am not able to use Matplotlib in this environnement so this not a Stata problem anymore. And I have to fix this (what a mess !) before being able to try and find an answer to my initial question......

      Comment


      • #4
        outside my OS, but the following seems relevant: you may try matplotlib.use('TKAgg') or "conda install python.app and launch your script with pythonw instead of python". And you can ask https://www.stata.com/support/tech-support/contact/

        Comment

        Working...
        X