Python - percentage symbol on imports - python

I'm following this tutorial and I've tried to copy the code of the first example.
this is the first lines on the example:
from PIL import Image
import random
import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow, show
import matplotlib.animation as animation
#this is crucial to animation in matplotlib
%matplotlib notebook
.
.
.
As you can see, right underneath the imports, there is this line %matplotlib notebook. this line is causing a problems because it doesn't recognized as a command.
this is the problem description: Statement expected, found Py:PERC and End of statement expected
My question is - what is the meaning of the percentage symbol in this case and why doesn't it work?

This syntax is a Jupyter notebook convention. If you are not using Jupyter notebook, then you can probably remove this line.

Related

my pandas and seaborn comands not responding

import pandas as pd
dataFrame = pd.read_excel("C:/Users/****/desktop/python folder/tensorflow/sheet.xlsx")
import seaborn as sbn
import matplotlib.pyplot as plt
sbn.pairplot(dataFrame)
Output is: PS C:\Users \ -----\Desktop\python folder> & C:/Users/------/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/-----/Desktop/python folder/tensorflow/tensorflow-101.py"
For your problem, try this.
import seaborn as sbn
import matplotlib.pyplot as plt
sbn.pairplot(dataFrame)
plt.show()
Graph will appear in new window, I guess. It's why your code is working in jupyter, and not in script.
ps. vsc is just a editor, so you can't use vsc for executing python. You are executing python script with your own python program.

jupyter notebook won't show images

#%%
from Utils.ConfigProvider import ConfigProvider
import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
config = ConfigProvider.config()
and
#%%
inspected = cv2.imread(config.data.inspected_image_path, 0)
reference = cv2.imread(config.data.reference_image_path, 0)
diff = np.abs(inspected - reference)
plt.figure()
plt.title('inspected')
plt.imshow(inspected)
plt.show()
note config.data.inspected_image_path and config.data.reference_image_path are valid paths.
No errors appear, but no images are shown as well.
Running the same code from a python file does show the image.
I have something missing from the notebook.
This happens both when running using jupyter notebook and directly from PyCharm (pro)
How do I get to see images? all other answers I found just tell me to plt.show() but this obviously does not work.
I don't mind a cv2 solution as well.
You need to set a matplotlib backend.
You can do this with
%matplotlib inline
If you want to be able to interact with the plot, use
%matplotlib notebook

How to resize a Python matplotlib.pyplot.figure already created

I'm working with this custom Py visual in PowerBI. Unfortunately, Power BI has some Python code leading my code that pre-defines the image size (5.55555555555556,4.16666666666667).
The result is a small image surrounded by a lot of empty space:
Is there any way I can redefine the size of the image, even though I cannot modify the leading code?
Any other suggestions are welcome,
Thanks!
# Prolog - Auto Generated #
import os, uuid, matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
import pandas
os.chdir(u'C:/Users/USER/PythonEditorWrapper_443a6d71-c4cc-4e62-ac6f-2dad3eeace3d')
dataset = pandas.read_csv('input_df_d2b6d8be-2212-4ece-902c-f85219eff22b.csv')
matplotlib.pyplot.figure(figsize=(5.55555555555556,4.16666666666667), dpi=72)
matplotlib.pyplot.show = lambda args=None,kw=None: matplotlib.pyplot.savefig(str(uuid.uuid1()))
#My code starts here, I cannot modify anything above this line.
#I wish I could add a line here to redefine figsize=(5.55555555555556,4.16666666666667)
from matplotlib import pyplot as plt
plt.figure(figsize=(15,20))
try this

Figure not displayed with matplotlib.use('Agg')

I work with matplotlib. When I add the following lines, the figure is not displayed.
import matplotlib
matplotlib.use('Agg')
here is my code :
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plot
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plot.figure(figsize=(12,9))
def convert_sin_cos(x):
fft_axes = fig.add_subplot(331)
y = np.cos(x)
fft_axes.plot(x,y,'g*')
for i in range(3):
fft_axes = fig.add_subplot(332)
x=np.linspace(0,10,100)
fft_axes.plot(x,i*np.sin(x),'r+')
plot.pause(0.1)
convert_sin_cos(x)
Thanks
That's the idea!
When I run your code, the console says:
matplotlibAgg.py:15: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
How can it be useful? When you're running matplotlib code in a terminal with no windowing system, for example: a cluster (running the same code with different inputs, getting lot of results and without the need to move the data I can plot whatever I need).

plot, figure, and title function error

I'm having trouble plotting my results in Python, both in Ubuntu 14 and Windows 7 (both 64bit). As a simple comparison I did:
from tvb.simulator.lab import *
--> to import (among others) numpy as np and matplotlib.pyplot.
x = [1,2,3]
plot(x)
--> NameError: name 'plot' is not defined
When I looked up this error (plot is not defined) and followed these instructions, I get this result
matplotlib.lines.Line2D object at 0x7f8e31754dd0
without output...
Anyone who knows how I can fix this?
Assuming that your import (tvb.simulator.lab) does
import numpy as np
import matplotlib.pyplot
then you have to call plot like this:
matplotlib.pyplot.plot(x)
BUT, you could also reimport it in your script:
import matplotlib.pyplot as plt
and then use the alias plt (thats farely common):
plt.plot(x)

Categories

Resources