How to add matplotlib from python3.6 to Blender? - python

In Window, I installed python3.6
and, installed matplotlib by commandline:
pip install matplotlib
After installation, I was running matplotlib in python console,
import matplotlib.pyplot as plt
import matplotlib.pylab as plb
In python36\lib\site-packages
I want in blender to use the matplotlib library in python,
Can I copy the matplotlib in python36\lib\site-packages of Python into the Blender\2.78 \python\lib\site-packages?
I tried copying but only importing matplotlib, but when `import matplotlib.pylot as plt the error is as follows
I do not know how to fix it, please help me
Thank you so much!

First you will want to make sure you use the same python version as blender was built with, for blender 2.78 that is python 3.5. For compiled python modules like matplotlib this is more important than pure python modules.
There are few ways to use third party modules, first like you are trying, is to install them into blender's copy of python. Another way is to delete blender's python so that it uses the system installed version. You can also adjust sys.path to allow blender's python to find third party modules.

Related

How do I run a Python Script in PowerBI?

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

How can you import a local version of a python package?

I think I've found a bug in matplotlib. I'm using anaconda as a package manager, but had to download matplotlib from github in order to edit it.
How do I import my modified version of matplotlib in order to test it? I tried using
import /absolute/path/to/modified/matplotlib
, but that didn't work. Ideally I would like to create a conda environment that uses the modified matplotlib instead of the original, so I can easily switch between the two.
How do you test and run a modified version of an open source library, without messing up your original version of the package? Is there a way to import a library from an absolute path?
Try this
import sys
sys.path.append('/absolute/path/to/modified/matplotlib')
import matplotlib # modified
Another option not mentioned, if you just put the matplotlib module (copy or move) in the directory of your project, python will check there first, find the version you put there, and look no further. This is exactly the reason why you shouldn't name your files, for example, math.py.
You can install a local version by telling anaconda to install the tar-ball of the package directly, i.e.
conda install package-version-py27.tar.bz2
You might also be able to use the --use-local argument.
See: https://github.com/conda/conda/issues/5266, https://github.com/conda/conda/issues/1884

matplotlib - no module named tkinter in Blender python

I am trying to write a blender script for which I need matplotlib.pyplot. When trying to import matplotlib.pyplot as plt I get the following error:
ImportError: No module named 'tkinter'
However, using the installed anaconda version, the import is no problem. The common solution to run
sudo apt-get install python3-tk
does not solve the problem.
I tried to add the Path to tkinter with:
sys.path.append('/usr/lib/python3.4/tkinter/')
sys.path.append('/home/<username>/anaconda3/lib/python3.6/tkinter/')
Both commands did not resolve the error.
I managed to import tkinter (and use it with matplotlib.pyplot) from Blender as I explained here:
https://stackoverflow.com/a/56050748/4511978
Hope it helps!
Andres
On Linux, you shouldn’t need to mess around with pip to install tkinter, since it’s a standard Python module. Also the Blender package should use the system Python, so it will have access to all system Python modules.
But ... tkinter is a GUI framework, and trying to use this will conflict with Blender’s own GUI.
Another option might be to use Matplotlib in offscreen-plotting mode, which doesn’t need a GUI.
Try switching the backend from tkinter to agg using -
matplotlib.use('agg')

how to include updated library in python code without root access?

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

Trouble in installing Basemap in 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.

Categories