Python Forum
Error with MatPlotLib subplots
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error with MatPlotLib subplots
#1
Question 
Hi.

I've been trying to create subplots with MatPlotLib, FinPlot, Ta-Lib, and Seaborn. Constant errors. I've tried Python V 3.9.x and 3.10.x using both PyCharm and Visual Studio Code using my own code and multiple samples of code copied from the internet. Here is one sample:

    # ----------- Test ------------
    
    plt.rcParams["figure.figsize"] = [7.50, 3.50]
    plt.rcParams["figure.autolayout"] = True

    fig, (ax1, ax2) = plt.subplots(2)
    df = pd.DataFrame(dict(name=["Joe", "James", "Jack"], age=[23, 34, 26]))

    df.set_index("name").plot(ax=ax1)
    df.set_index("name").plot(ax=ax2)

    plt.show()

    # ----------- End Test ------------
Note that Matplotlib and pandas are declared at the top of the script:
import matplotlib.pyplot as plt
import pandas as pd 
When executed, it throws this error on the "df = pd.DataFrame(..." command:
Error:
local variable 'pd' referenced before assignment
The code comes from: Tutorials Point - Article "Stuffing a Pandas DataFrame.plot into a Matplotlib subplot". This seems like a reputable site and presumably, the code should work.

In another test function that I created using a datafreme loaded with a custom CSV file, this code:
   
    # ...
    fig, (ax1, ax2) = plt.subplots(2)

    df_cur_daily("Close").plot(ax=ax1)
    df_cur_daily("High").plot(ax=ax2)
    plt.show()
Throws this error when it hits
df_cur_daily("Close").plot(ax=ax1)
Error:
'DataFrame' object is not callable
I've been banging my head on this for hours on many tests like this. I'd appreciate any help. Is there any reason this python code will not work correctly in VS Code? Another approach to plotting a chart? Another library?

Thanks for any advice,

Dan.

p.s. Platform Specs:
  • Apple Macbook Pro M1 Max (latest)
  • 32 GB
  • Python 3.9.10
  • MacOS Monterey V12.3
  • Visual Studio Code V1.65.2 (Universal)
Reply


Messages In This Thread
Error with MatPlotLib subplots - by Danno - Mar-22-2022, 04:15 AM
RE: Error with MatPlotLib subplots - by Larz60+ - Mar-22-2022, 08:08 AM
RE: Error with MatPlotLib subplots - by deanhystad - Mar-22-2022, 11:48 AM
RE: Error with MatPlotLib subplots - by Danno - Mar-23-2022, 05:07 AM
RE: Error with MatPlotLib subplots - by Danno - Mar-23-2022, 04:52 PM
RE: Error with MatPlotLib subplots - by Larz60+ - Mar-23-2022, 08:07 PM
RE: Error with MatPlotLib subplots - by Danno - Mar-26-2022, 07:31 AM
RE: Error with MatPlotLib subplots - by Larz60+ - Mar-26-2022, 08:32 AM
RE: Error with MatPlotLib subplots - by Danno - Apr-01-2022, 12:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,804 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,809 Aug-19-2022, 11:07 AM
Last Post: dm222
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,360 Mar-11-2021, 10:52 AM
Last Post: buran
  Error message when trying to install Matplotlib in Python3.8 Transitionyte 5 5,312 Jan-22-2020, 11:52 PM
Last Post: Transitionyte
  matplotlib pip install error Paul_Grosvenor 3 4,332 Nov-18-2019, 01:11 PM
Last Post: buran
  how to create subplots in for loop? python_newbie09 1 5,976 Sep-25-2019, 02:29 PM
Last Post: stullis
  matplotlib recursion error when repeatedly displaying a surface AdeIsHere 0 1,983 Sep-19-2019, 04:36 PM
Last Post: AdeIsHere
  Subplots on one figure SarahColombo96 0 1,796 Feb-09-2019, 01:09 PM
Last Post: SarahColombo96
  Change distance and title postion in subplots Mark3232 2 2,885 Jul-19-2018, 10:10 AM
Last Post: Mark3232
  Help with plot legends in subplots Felipe 2 5,944 Feb-06-2017, 02:55 PM
Last Post: Felipe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020