I am attempting to save matplotlib animations to a movie via ffmpeg on Ubuntu 12.04 LTS (32-bit Desktop). Following the matplotlib example, it fails to load the animation writer: AttributeError: 'module' object has no attribute 'writers' (line 15 of the example):
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update_line(num, data, line):
line.set_data(data[...,:num])
return line,
# Set up formatting for the movie files
Writer = animation.writers['ffmpeg']
Via apt-get, I've tried installing ffmpeg, every codec imaginable, and even tried to compile ffmpeg from source. Nothing works.
How do I get matplotlib to talk to ffmpeg on Ubuntu?
If you are using the unbuntu packaged version of matplotlib it is 1.1.1rc1. The attribute writers was added about 3 months after that tag, and is in versions 1.2 and later.
You can either install matplotlib from source (this is what I do, it's not too bad) or use the daily ppa.
My advice for compiling from source is to use the packaging system for as many of the dependencies as possible and install matplotlib by hand (if you do want to use pip see this answer) as such
git clone git://github.com/matplotlib/matplotlib.git
cd matplotlib
git checkout -b v1.2.0
python setup.py install --prefix=/home/username/local_installs/
(which will get you the latest stable version) then make sure the path where it got installed is in your $PYTHONPATH which can be done by including the line
export PYTHONPATH=/home/username/local_installs/lib/python2.7/site-packages/:$PYTHONPATH
in your ~/.bashrc file. You might have to vary that line a bit depending on which version of python you use. You might need to do this (and make sure folders exist) before setup.py will be happy.
Related
I am new to Python. I am trying to create a Bar Chart in Power BI using Python but the script does not seem to work. The code is below:
pip install pandas
pip install matplotlib
import matplotlib.pyplot as plt
plt.plot(dataset.Letter,dataset.Number)
plt.show()
Please find attached the file:
https://www.mediafire.com/file/f0bwt9cp4ha1sfb/Example.pbix/file
enter image description here
After installing only one (1) initial time pip install pandas and pip install matplotlib, you only need to write in your code:
import matplotlib.pyplot as plt
dataset.plot('bar', dataset.Letter, dataset.Number)
plt.show()
import is for setting up the library and you must always use it in your code, while pip install pandas and pip install matplotlib only need to be installed once at the beginning.
First, you should not be required to run pip install every time you run your script. You should only have to use pip install commands when installing a library for the first time. This might require you ensuring that you have the correct libraries located on your computer. However, I do not know what your error is since I do not have your error code.
Also, be sure to specify what type of plot you wish to produce from matplotlob.
Try (EDIT):
import matplotlib.pyplot as plt
dataset.plot(kind='bar', x='Letter', y='Number')
plt.show()
You do not need an indent in your last two lines of code.
Also after reviewing you update I think it might be best if yo review the matplotlib library documentation and familiarize yourself with Python basics. You can learn Python almost anywhere, I have found the interactive lessons on w3schools.com to be useful.
matplotlib: https://matplotlib.org/stable/users/index.html
I know this question is asked a lot, I searched for the last three hours for an answer, but couldn't solve my problem.
As soon as I try to:
import matplotlib.pyplot as plt
my Visual Studio Code IDE tells me that: Unable to import 'matplotlib.pyplot'
My current version of Python is:
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
as you can see, I used the Anaconda package, hence matplotlib should be included. I am Using Mac OS 10.15.2
If I enter
import matplotlib.pyplot as plt
it does import that, however, as soon as I try a basic example, like
x = [1,2,3]
y = [2,4,1]
plt.plot(x, y)
I obtain:
[<matplotlib.lines.Line2D object at 0x10c23d950>]
And a blank token in my Dashboard pops up, which I can only force to quit.
In the course of trying to solve this problem, I tried anything I saw that was related to this topic, although I did not understand everything of it. I hope I did not make a real damage.
My last step was completely deinstalling anaconda and reinstalling.
Thanks for taking your time!
The module can be imported, but your IDE says module not found, means your linter (vscode uses pylinter) is not configured correctly.
Start PyLint from correct anaconda environment in Visual Studio Code
First you need to install package matplotlib using conda console in your project
conda install -c conda-forge matplotlib
You also can install package using PIP
python -m pip install -U matplotlib
And Finally import your package in your source code
import matplotlib.pyplot as plt
Or another Way you can import
from matplotlib import pyplot as plt
Having come here with this error myself, especially if you're using VS Code you need to make sure that the version of Python you're running in VS Code is the same one that's running on your OS. Otherwise, you can conda install or pip3 install all you want into the OS, but VS Code and Jupyter Notebooks won't know anything about those modules. There are a few ways to figure this out, but mostly it's making sure that the Python version you see when typing python --version in the terminal matches the interpreter shown in the top-right corner of your VS Code window.
I normally use PyCharm for python coding, but just for the heck of it, I tried to use Visual Studio Code today, and I'm having some problems.
So I followed the steps shown in the "Getting Started with Python in VS Code" page, and I copied this to my new python project:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(0, 20, 100) # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x)) # Plot the sine of each x point
plt.show() # Display the plot
just to test if it works well, and for some random reason, whenever I run this code through terminal/cmd in VSCode, I get this:
ImportError: Missing required dependencies ['numpy']
BUT when I use the Debug Mode, it seems to work perfectly fine.
similar thing happened when I tried to run my previous projects through VSCode. So I thought maybe it was just the problem with my environment, so I changed it to the one where I have my tools installed, but nope, I still got the error.
I tried uninstalling then installing again and that didn't work as well.
I Seriously don't know what's happening right now. Why does it work well in Debug Mode but not in terminal/cmd? does anyone know what to do in this situation?
Thanks!
...In Python, packages are how you obtain any number of useful code libraries, typically from PyPI. For this example you use the matplotlib and numpy packages to create a graphical plot as is commonly done with data science. (Note that matplotlib cannot show graphs when running in the Windows Subsystem for Linux as it lacks the necessary UI support.)
Return to the Explorer view (the top-most icon on the left side, which shows files), create a new file called standardplot.py, and paste in the following source code:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
x = np.linspace(0, 20, 100) # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x)) # Plot the sine of each x point
plt.show() # Display the plot
Tip: if you enter the above code by hand, you may find that auto-completions change the names after the as keywords when you press Enter at the end of a line. To avoid this, type a space, then Enter.
Next, try running the file in the debugger using the "Python: Current file" configuration as described in the last section. (If you still have "stopOnEntry": true in that configuration, you need to select the run command again to continue.)
Unless you're using an Anaconda distribution or have previously installed the matplotlib package, you should see the message, "ModuleNotFoundError: No module named 'matplotlib'". Such a message indicates that the required package isn't available in your system.
To install the matplotlib package (which also installs numpy as a dependency), stop the debugger and run Terminal: Create New Integrated Terminal from the Command Palette (⌃⇧(Windows, Linux Ctrl+Shift+))). This command opens a command prompt for your selected interpreter. Then enter the following commands as appropriate for your operating system (commands may require elevation if the Python interpreter is installed in a protected area of the file system):
Note: If you are unable to install the package or encounter other problems, please file an issue on GitHub so we can help you investigate.
# Don't use with Anaconda distributions because they include matplotlib already.
# macOS
sudo python3 -m pip install matplotlib
# Windows (may require elevation)
py -3 -m pip install matplotlib
# Linux (Debian)
sudo apt-get install python3-tk
python -m pip install matplotlib
from: https://code.visualstudio.com/docs/python/python-tutorial
Got this error and was able to fix it by running conda init in the Python Debug Console terminal, and then closing the terminal before starting a new debug session.
In VS Code undee Windows open Terminal -> New Terminal and run
pip3 install pandas
and then
pip3 install matplotlib
While installing pandas, numpy will be installed as well
I believe it may have something to do with the PATH variables. I just had the same problem in Windows 10 and found out that if I launch VS Code through an anaconda prompt it works just fine, no need to mess around with the PATH variables.
So, instead of opening VS Code through the start menu, just open an anaconda prompt (or anaconda shell), cd to your directory and type code .. That solved my issue.
I am working remotely on university systems.I wanted to include python's matplotlib1.1 and university system has matplotlib 0.98.
I did according to this post
How to install python modules without root access?
-bash-4.1$ easy_install --install=$HOME/lib/python2.6/site-packages matplotlib
Searching for matplotlib
Best match: matplotlib 0.98.3
Adding matplotlib 0.98.3 to easy-install.pth file
Using /opt/python/2.6/lib/python2.6/site-packages
Processing dependencies for matplotlib
Finished processing dependencies for matplotlib
But still I am not able to use modules in new version
I am getting importerror for animation which is there only in updated version.
Try the following import to import a specific version.
import pkg_resources
pkg_resources.require("matplotlib==0.98.3")
import matplotlib
I am trying to install Basemap, and beforehand I already have the prerequisite versions of Matplotlib, Python and Numpy working on my Mac. To install Basemap I am following the instructions in this website http://matplotlib.org/basemap/users/installing.html
I seemed to progress quite well with the steps, until the very end which is "Check your installation by running from mpl_toolkits.basemap import Basemap at the python prompt." I got "ImportError: cannot import name pyproj" from a line that reads "---> 30 from mpl_toolkits.basemap import pyproj".
Does anyone have any idea? Many thanks!
The compiled module could be accessible by python. To achieve that you should put the module into python path or you need to add the location of the module to PYTHONPATH environment variable.
To see the python installations paths you can write these lines to python shell:
import sys
for path in sys.path:
print(path)
The code will show the paths python is looking for modules.
After you compile the c library, you need to go to upper folder and run python setup.py install as it's said in the installation page. This will put python modules into one of your python paths.
I had the same problem installing basemap-1.0.7.
I found that the file
/usr/lib64/python2.6/site-packages/mpl_toolkits/basemap/init.py
had a reference to axes-grid1, but python lists only module axes_grid.
So I changed grid1 to grid and now basemap imports without error.
Had the same issue on OSX, found after much faffing that Basemap worked fine with a non-native version of python (for me 2.7.12 with everything under /opt/local/Library/Frameworks...) but not with the native version (for me 2.7.10 with most stuff under /System/Library/Frameworks... and Basemap under /Library/...). I did note also that under the native version there was already some mpl_tooklit content and without the permission to add Basemap there you end up with mpl_toolkit contents in multiple places. I wasn't sure if this was the problem specifically but as I said having it all under opt using non-native python was what solved this for me.