Yesterday learning Pandas and matplotlib process, in Jupyter notebook encountered importerror:matplotlib is required for plotting error, the following is a specific description of the problem, in this record, Provide a reference for friends who study in the back.
Environment
win8.1, python3.7, Jupyter Notebook
Problem description
1 ImportPandas as PD2 ImportMatplotlib.pyplot as Plt3DF = Pd.read_csv (r"D:\Data\percent-bachelors-degrees-women-usa.csv")4Df.plot (x =" Year", y ="Agriculture")5Plt.xlabel (" Year")6Plt.ylabel ("Percentage")7Plt.show ()
Execute the above code in Jupyter notebook, throwing the following error:
Importerror:matplotlib is required for plotting
Solution Ideas
1. Cannot import matplotlib? Under cmd Command Window confirm:
Without an error, the installation was successful and could be successfully imported.
2. Try another way: Before using the plot () method drawing in Pandas, replace it with the plot () method in Matplotlib.pyplot
1 ImportPandas as PD2 ImportMatplotlib.pyplot as Plt3DF = Pd.read_csv (r"D:\Data\percent-bachelors-degrees-women-usa.csv")4Df_year, df_agriculture = df[" Year"], df["Agriculture"]5Plt.plot (Df_year, Df_agriculture,"-", color ="R", LineWidth = 5)6Plt.show ()
To run successfully in Jupyter notebook:
Run the Pandas plot () method again, still error, and check again for no errors in the found statement.
So the question is, why doesn't the plot () method in pandas work?
3. Change the IDE to see if you can run in Pycharm:
1 ImportPandas as PD2 ImportMatplotlib.pyplot as Plt3DF = Pd.read_csv (r"D:\Data\percent-bachelors-degrees-women-usa.csv")4Df.plot (x =" Year", y ="Agriculture")5Plt.xlabel (" Year")6Plt.ylabel ("Percentage")7Plt.show ()
In the Pycharm can run successfully, and in Jupyter notebook can not run, look at the problem of the IDE, then there are differences between the two:
As far as my personal computer is concerned, Pycharm is what I just started (after installing the Matplotlib), and Jupyter notebook has not been shut down for several days (the installation is not closed before and after Matplotlib), in order to ensure that the conditions are uniform, Try to focus on starting the Jupyter notebook.
Restart Jupyter Notebook After successful running the code again:
1 ImportPandas as PD2 ImportMatplotlib.pyplot as Plt3DF = Pd.read_csv (r"D:\Data\percent-bachelors-degrees-women-usa.csv")4Df.plot (x =" Year", y ="Agriculture")5Plt.xlabel (" Year")6Plt.ylabel ("Percentage")7Plt.show ()
To successfully display:
It looks like the problem is that Jupyter notebook was not restarted after installing Matplotlib.
Summarize
Personal conjecture: When using the plot () method in pandas, the Pyplot drawing frame in Matplotlip is only used to display graphics, and to make the two interactive, it should be ensured that both are installed successfully before starting the IDE.
If you encounter a similar problem later, if you are sure that the code is correct, you can sometimes resolve the problem more quickly by trying to restart the IDE directly.
Troubleshoot Importerror:matplotlib is required for plotting problems encountered in Jupyter notebook