How do I run a Python Script in PowerBI? - python

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

Related

How do I get Pyinstaller to work with pyplot?

I am trying to turn a program, that uses the matplotlib.pyplot module, into an executable.
The script itself works as intended when run in the terminal, but when I use the executable made by pyinstaller, it shows - "Script failed to execute".
I have narrowed down the issue to the following code:
import matplotlib.pyplot as plt
The .exe will run fine with this code commented out, but that way I am not able to plot graphs using pyplot.
EDIT: I did some research on this and found out it's a known issue with Matplotlib 3.3, so I was able to get around it by uninstalling the current version and using:
pip install matplotlib==3.1.3

Unable to import 'matplotlib.pyplot'

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.

New To Visual Studio Code, having problems with pandas/numpy

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.

How to add matplotlib from python3.6 to Blender?

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.

Matplotlib animation MovieWriters fails on Ubuntu 12.04

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.

Categories