This is my first time asking a question. I am trying to use "pysheds" to analyze some hydrologic DEM files. The developer as some pretty thorough "how to" videos but when I try to load the DEM file in the way that they show I get the following error:
module 'pysheds.grid' has no attribute 'from_raster'
here's my code
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import geopandas as gpd
import pysheds
import pysheds.grid as Grid
import mplleaflet
grid = Grid.from_raster('path.tif', data_name = 'dem')`
I checked the print(dir(Grid)) in the console and didn't see this attribute listed.
Am I missing something?
Thanks!
According to documentation, you should import Grid from pysheds.grid like this:
from pysheds.grid import Grid
grid = Grid.from_raster('n30w100_con', data_name='dem')
grid.read_raster('n30w100_dir', data_name='dir')
grid.view('dem')
instead of
import pysheds.grid as Grid
Related
I am trying to define a function using cwt in Python, however, I get the error message
global name 'cwt' is not defined
This is the code that I am using:
import os
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.pyplot import specgram
import librosa
import librosa.display
import numpy as np
from ssqueezepy import cwt
from ssqueezepy.visuals import plot, imshow
from numba import jit
#jit
def scaleogram_convert(data):
Wx, scales = cwt(data, 'morlet')
Wx = abs(Wx) # remove complex component
return Wx
img3 = scaleogram_convert(trimmed)
How can I get this function to work correctly when called?
Any help is greatly appreciated!
i'm having what i think is a gtk error in python while trying to recognize some faces and then apply a gaussian blur, i'm new in python! Here is the error:
*Gtk-WARNING *: 00:11:03.559: Theme parsing error: gtk.css:6321:10: "height" is not a valid property name
i'm currently using Archlinux and python 3+, i checked ~/.config and my Gtk is 2.0, idk if that is the issue and i also don't know how to change it/ update it.
Here is the code i'm triying to compile:
from skimage.filters import gaussian
from skimage import color
from skimage import data
import scipy.misc
import os
import sys
import plots
import matplotlib.pyplot as plt
from PIL import Image
path = "/home/carlos/python/img/jaja.jpeg"
img = plt.imread("/home/carlos/python/img/girl.jpeg")
#plt.show(img)
trained_file = data.lbp_frontal_face_cascade_filename()
detector = Cascade(trained_file)
detected = detector.detect_multi_scale(img=img, scale_factor=1.2, step_ratio=1, min_size=(50,50), max_size=(200,200))
print(detected)
I am having an issue where I cannot call the 'pyplot' element of 'matplotlib'. From the code below you can see I've had to add a "TkAgg" for the mattplotlib element to work, which is a common issue.
import matplotlib
matplotlib.use("TkAgg")
However, now I cannot add the '.pyplot' to the import. I've tried the following:
import matplotlib.pyplot as plt
plt.use("TkAgg")
But this gives me the error:
AttributeError: module 'matplotlib.pyplot' has no attribute 'use'
How can I get around this as my code requires pyplot to function, but I cannot work out how to import it while still having to use ".use("TkAgg").
I am running Python 3.6.2 and I am using Tkinter to develop my program
Those are two entirely different things. You import matplotlib to be able to set the backend. Then you need to still import pyplot to be able to use it afterwards.
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
# ... rest of code
If you use the use() function, this must be done before importing matplotlib.pyplot. Calling use() after pyplot has been imported will have no effect.
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
check with :
matplotlib.get_backend()
I have been trying to get an updated plot from a pandas dataframe without success. My problem: the plot window does not appear (it is not hidden - I am sure about that).
I already tried to rebuild and change different solutions from stackoverflow. My most recent try is based on this post. Pure copy,paste does work, so the problem needs to be in my modifications.
I changed it to this as I want to update it automatically every second.
import serial as s
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from time import sleep
data = pd.DataFrame(np.random.random((10,10))) # I want to use pandas because
# of easier timestamp handling
fig, ax = plt.subplots()
ax.set(title='title')
im = ax.imshow(data)
while True:
im.set_data(np.random.random((10,10)))
print "hello" #just to see if sth happens
fig.canvas.draw()
sleep(1)
plt.show()
Just to explain: Later I want to read data from serial ports and feed them to the plot to get my data visualized.
Well, what you expect: the provided code does print hello each second but does not show me any plot. Any ideas? I am out of them.
By the way: I am surprised that there is no "easy, straight forward" solution for this kind of problem to be found. I can imagine, there is some people who are trying to do updated plots?!
you can use the package drawnow
from pylab import * # import matplotlib before drawnow
from drawnow import drawnow, figure
from time import sleep
import numpy as np
def draw_fig_real():
imshow(data, interpolation='nearest')
data = np.random.random((10,10))
figure()
for i in range(10):
data = np.random.random((10,10))
sleep(0.5)
drawnow(draw_fig_real)
Hope this helps
I'm trying to make a plot:
from matplotlib import *
import sys
from pylab import *
f = figure ( figsize =(7,7) )
But I get this error when I try to execute it:
File "mratio.py", line 24, in <module>
f = figure( figsize=(7,7) )
TypeError: 'module' object is not callable
I have run a similar script before, and I think I've imported all the relevant modules.
The figure is a module provided by matplotlib.
You can read more about it in the Matplotlib documentation
I think what you want is matplotlib.figure.Figure (the class, rather than the module)
It's documented here
from matplotlib import *
import sys
from pylab import *
f = figure.Figure( figsize =(7,7) )
or
from matplotlib import figure
f = figure.Figure( figsize =(7,7) )
or
from matplotlib.figure import Figure
f = Figure( figsize =(7,7) )
or to get pylab to work without conflicting with matplotlib:
from matplotlib import *
import sys
import pylab as pl
f = pl.figure( figsize =(7,7) )
You need to do:
matplotlib.figure.Figure
Here,
matplotlib.figure is a package (module), and `Figure` is the method
Reference here.
So you would have to call it like this:
f = figure.Figure(figsize=(7,7))
To prevent future errors regarding matplotlib.pyplot, try doing this:
import matplotlib.pyplot as plt
If you use Jupyter notebooks and use: %matplotlib inline, make sure that the "%matplotlib inline FOLLOWS the import matplotlib.pyplot as plt
Karthikr's answer works but might not eliminate errors in other lines of code related to the pyplot module.
Happy coding!!
Instead of
from matplotlib import *
use
import matplotlib.pyplot as plt
For Jupyter notebook I solved this issue by importig matplotlib.pyplot instead.
import matplotlib.pyplot as plt
%matplotlib notebook
Making plots with f = plt.figure() now works.
Wrong Library:
import matplotlib as plt
plt.figure(figsize=(7, 7))
TypeError: 'module' object is not callable
But
Correct Library:
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 7))
Above code worked for me for the same error.