I'm taking a course on Coursera and doing an assignment after watching a teaching video.
I wanna know why it shows "NameError: name 'histogram' is not defined" after running my own code.
Add histogram library in your code. please add line in the starting of the code. I should work properly.
from histograms import histograms
You'll need to import the Histogram function as it is not built into Python. There are a few different packages that offer histograms -- for example pyplot and numpy. Here's an example using numpy
import numpy as np
np.histogram([1, 2])
Documentation: https://numpy.org/doc/1.18/reference/generated/numpy.histogram.html
Related
I'm fairly new to data analysis with matplotlib and I was hoping I could get some guidance as to where I'm going wrong. As part of a personal project, I've made a face mask detection system using a raspberry pi following the tutorial on https://www.tomshardware.com/how-to/raspberry-pi-face-mask-detector . This part works great, however I was hoping to take it a step further by using the data it produces to form a graph of mask adherence over a period of time
In my attempt I created 2 variables, "mask_off" and "mask_on". When a person is detected wearing a mask or not, the corresponding value increases by 1. From here I was hoping to write these variables to a csv file and plot a graph from there using the values. The code I've written below doesn't seem to run at all:
from time import sleep, strftime, time
import matplotlib.pyplot as plt
import numpy as np
with open('home/pi/face_mask_detection/mask_adherence.csv', 'a') as log:
while True:
nwm = mask_off
wm = mask_on
y.append(mask_off)
x.append(mask_on)
plt.clf()
plt.scatter(x, y)
plt.plot(x,y)
log.write('{0},{1}\n'.format(strftime('%Y-%m-%d %H:%M:%S'),str(nwm)))
log.write('{0},{1}\n'.format(strftime('%Y-%m-%d %H:%M:%S'),str(wm)))
plt.pause(1)
plt.draw()
When I run the code I only get "Import Error: numpy.core.multiarray failed to import" so I can't even test if the code actually works. Chances are I'm taking the complete wrong approach to this so any kind of guidance, advice or tips would be greatly appreciated. Thanks.
I am using interactive python with plt.ion() for generating figures (v2.7) and have noticed that the figure looks different from the figure exported by savefig (this is not a DPI issue (cf. matplotlib savefig() plots different from show()) - I think it might be a backend issue, but would appreciate help as I don't understand this properly).
Specifically, I wanted visualise the importance of a series of points by the intensity of their colour, which I thought I could do with the "alpha" keyword in matplotlib.
When I just do this, this works fine,
but when I want to add a line to the figure, the alpha keyword seemed to not work any more, and plt.ion() shows this:
I initially thought that perhaps the following issue on github may be related:
https://github.com/matplotlib/matplotlib/issues/4580
but then I noticed that exporting the figure actually produced the following file (i.e. as desired):
It would be great to understand a bit better what is going on, and how I can avoid such issues in the future. Is plt.ion()/plt.show() not the best way to show figures in interactive python, or is this an issue with the alpha keyword?
The code is here:
import numpy as np
from numpy import random as random
from matplotlib import pyplot as plt
fig2,ax2=plt.subplots(1,1,figsize=(3,3),sharey=True)
for ii in range(1):
ax2.plot(np.linspace(0,200,200), [0.1]*200, c= 'k')
for i in range(200):
test2=random.randint(5)
ydata= random.rand(test2)
test = random.rand(test2)
for j in range(test2):
ax2.plot(i,ydata[j],'o',ms=4, c= 'Darkblue',alpha=test[j],markeredgecolor='None')
i follow the google course about machine learning. i'm on this part : pandas
But on my mac when i want to generate a chart with this command :
california_housing_dataframe.hist('housing_median_age')
it doesn't work. The python icon appear but nothing is displaying on the screen.
i have see some tips with the backend parameter into matplotlibrc but mine is equals to MacOSX and it should work ?
Thanks for help
To elaborate on the comment from T. Kelly:
You need to call plt.show(). This worked for me:
import matplotlib.pyplot as plt
california_housing_dataframe.hist('housing_median_age')
plt.show()
I'm following Google ML Crash Course(I think you are also following it based on the variable name).
I too encountered the same problem.
When I call
california_housing_dataframe.hist('housing_median_age')
It is not showing any histogram. Instead it is showing
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x12bb814e0>]],
dtype=object)
To show the histogram, add this line in your imports:
%matplotlib inline
It should show the histogram.
I am getting confused over all the different python datatypes. I wrote a face recognition algorithm in MATLAB, I want to redo it it Python. I am stuck trying to resize my images. How do I do that? Can someone link me to the things that i need to read to understand the difference in the functions between MATLAB in Python? Like MATLAB got imresize but Python got what?
Is there something like import all_matlab_functions so I can python in matlab language ?
I cant believe im struggling so hard. Help! My code below shows how stupid i am with python currently.
%matplotlib inline
import matplotlib.image as mpimg
import glob
import matplotlib.pyplot as plt
image_list = []
for filename in glob.glob('<directory>.pgm'):
im = mpimg.imread(filename)
image_list.append(im) # read all image
from PIL import Image
haha = image_list[1].resize((10 10), resample=0) # try to resize image BUT FAIL
using the jupyter qt-console interpreter, or jupyter notebook you can set the interpreter environment magic %pylab inline so that images will be displayed inline and the matplotlib.pylab namespace and numpy namespaces will be imported into the current namespace. All this means though is that you don't have to prefix the commands with matplotlib.pylab.plot or plt.plot, for example. You can just use plot().
%pylab inline
x = linsapce(1,2*pi,100)
y = sin(x)
plot(x,y)
matplotlib is meant to give good tools for plotting that look similar to matlab plotting, numpy for arrays and mathematics, and scipy gives you tools for more complex functions. For example, scipy.misc.imresize can do what you are looking for:
http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.misc.imresize.html
If you are looking for a direct way to run your matlab programs outside of matlab, you might want to try Octave instead.
https://www.gnu.org/software/octave/
I hope this question isn't too elementary. I've searched extensively for a solution but haven't discovered one yet.
I've recently begun using Jupyter Notebook with Sympy to take notes and do my homework in my Calculus II class (and what a HUGE BENEFIT this has been!).
However, my sole problem with it is that I'm unable to figure out how to configure the size (i.e. the dimensions in pixels) of the plot figure.
It's easy enough to do using matplotlib directly (matplotlib.pyplot.figure() specifically), but I'm using the Sympy.mpmath.plotmodule because Sympy works much better for the symbolic manipulation we're doing in this course. I know Sympy has its own plotting module, but the one in mpmath seems easier to use so far (with the exception of this one issue, of course).
However, I've looked through the mpmath documentation and have googled the problem repeatedly, without a solution.
How can you change the size of the image that results from plotting a function using the mpmath API?
You may try changing the size of sympy's plots via pyplot's rcParams:
import sympy
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = 10, 3
sympy.mpmath.plot([cos, sin], [-4, 4])