PyCharm - Auto Completion for matplotlib (and other imported modules)

前端 未结 2 694
滥情空心
滥情空心 2021-02-07 20:50

I am using PyCharm 2016.1 and Python 2.7 on Windows 10 and imported the matplotlib module.

As the matplotlib module ist very extensive and I am relatively new to Python

相关标签:
2条回答
  • 2021-02-07 21:21

    In addition to Paul's answer. If you are using fig, ax = plt.subplots() , you could use figure type hint. See below example:

    from matplotlib import pyplot as plt
    import matplotlib.axes._axes as axes
    import matplotlib.figure as figure
    
    fig, ax = plt.subplots()  # type:figure.Figure, axes.Axes
    ax.
    fig.

    0 讨论(0)
  • 2021-02-07 21:22

    I believe your problem is highlighted here:

    https://intellij-support.jetbrains.com/hc/en-us/community/posts/205816499-Improving-collecting-run-time-type-information-for-code-insight?sort_by=votes

    Tldr return types can vary, so it cant be figured out at compile time.

    Most accepted way is to use a type hint, since it can only figure out what type it as run time :

    import matplotlib.axes._axes as axes
    
    fig = plt.figure(figsize=(5,10))
    ax1 = fig.add_subplot(3,1,1) # type:axes.Axes
    ax1.set_xlabel('Test') <- now autocompletes
    

    You can also try an assert isinstance:

    import matplotlib.axes._axes as axes
    
    fig = plt.figure(figsize=(5,10))
    ax1 = fig.add_subplot(3,1,1)
    assert isinstance(ax1, axes.Axes)
    ax1.set_xlabel('Test')
    

    It wont find the autocomplete if you do it after the method you are looking for:

    ax1.set_xlabel('Test')
    assert isinstance(ax1, axes.Axes)
    

    With this, you shouldnt let isinstance dictate the control flow of your code, if you are trying to run a method that doesnt exist on an object, it should crash, however, if your different object has a method of the same name (!) then you have inadvertently reached that goal without annotations being there. So I like it better, since you want it to crash early and in the correct place. YMMV

    From the doc:

    Assertions should not be used to test for failure cases that can occur because of bad user input or operating system/environment failures, such as a file not being found. Instead, you should raise an exception, or print an error message, or whatever is appropriate. One important reason why assertions should only be used for self-tests of the program is that assertions can be disabled at compile time.

    If Python is started with the -O option, then assertions will be stripped out and not evaluated. So if code uses assertions heavily, but is performance-critical, then there is a system for turning them off in release builds. (But don't do this unless it's really necessary.

    https://wiki.python.org/moin/UsingAssertionsEffectively

    Alternatively, if you dont want to add to your code in this fashion, and have Ipython/jupyter installed through anoconda, you can get the code completion from the console by right clicking the code to be ran and choosing "execute selection in console"

    0 讨论(0)
提交回复
热议问题