Error "'Quiver' object has no attribute 'shape'" - python-2.7

I am using the Python Quiver function to combine u-velocity and v-velocity values into a vector map. I have semi-successfully combined the two arrays by using the quiver function within the colormesh() function, and my code returns a B&W plot of the data, but I get an error message "'Quiver' object has no attribute 'shape'". The data plot also seems to not allow the landmass/ocean commands to plot (or if I put those commands before the colormesh() command, then the quiver() command doesn't work). Also, I can't seem to add color or size to the quiver arrows. Why am I still getting sort of successful plotting, if I'm getting this error message? How can I fix it? I tried making a quiver array and then just plotting the name of that in the colormesh(), but that didn't work, either.
Thank you!!
m = Basemap(llcrnrlon=-120,llcrnrlat=32,urcrnrlon=-116,urcrnrlat=35,
resolution='l',projection='stere',
lat_0=32.5,lon_0=-117.)
fig = plt.figure(figsize=(10,10))
plt.figtext(.45,.15,'Figure 1. Avg. velocity from CORDC HF Radar avg. June 1, 2013',fontsize=12,ha='center')
x, y = m(lon,lat)# define color map
cmap = plt.cm.hsv
cs = m.pcolormesh(x,y,quiver(lon[0:230],lat[0:230],u_nanmean_mask,v_nanmean_mask),shading='gouraud',cmap=cm.RdYlBu,vmin=-0.10,vmax=0.12)
m.drawcoastlines()
m.fillcontinents(color='#989865',lake_color='w')
m.drawmapboundary(fill_color='w')
m.drawparallels(np.arange(-80.,81.,5.),labels=[0,1,1,0])
m.drawmeridians(np.arange(-180.,180.,5.),labels=[1,0,0,1])

Related

How to put parameters obtained through "pandas.describe" in a plot in one go?

Like if i have a data frame with four columns and i want to plot any two columns of it just to visualize my data. And we can find the value of all the parameters by using this
pd.describe()
count 332.000000
mean 5645.999337
std 391.081389
min 4952.290000
25% 5294.402500
50% 5647.905000
75% 6028.805000
max 6290.980000
Now, how can we put the information that we get with this function ('pandas.describe') into the plot in just one go. Instead of using the usual 'label' function from matplotlib.
Matplotlib has the option ax.text. So you need to convert this info into text.
Here comes an example:
import pandas as pd
df=pd.DataFrame({'A':[1,2,3]})
desc=df.describe()
Describe is also a DataFrame, you can turn every column into a string list:
data1=[i for i in desc.index]
data2=[str(i) for i in desc.A]
Now you can join both with a colon in between:
text= ('\n'.join([ a +':'+ b for a,b in zip(data1,data2)]))
Then in your graph, you can input:
ax.text(pos1, pos2, text , fontsize=15)
Where pos1 and pos2are numbers for the position of your text.
Does that help?
Tell me!

How to apply a mask to a DataFrame in Python?

My dataset named ds_f is a 840x57 matrix which contains NaN values. I want to forecast a variable with a linear regression model but when I try to fit the model, I get this message "SVD did not converge":
X = ds_f[ds_f.columns[:-1]]
y = ds_f['target_o_tempm']
model = sm.OLS(y,X) #stackmodel
f = model.fit() #ERROR
So I've been searching for an answer to apply a mask to a DataFrame. Although I was thinking of creating a mask to "ignore" NaN values and then convert it into a DataFrame, I get the same DataFrame as ds_f, nothing changes:
m = ma.masked_array(ds_f, np.isnan(ds_f))
m_ds_f = pd.DataFrame(m,columns=ds_f.columns)
EDIT: I've solved the problem by writing model=sm.OLS(X,y,missing='drop') but a new problem appears when I display results, I get only NaN:
Are you using statsmodels? If so, you could specify sm.OLS(y, X, missing='drop'), to drop the NaN values prior to estimation.
Alternatively, you may want to consider interpolating the missing values, rather than dropping them.

Adding data to a Pandas dataframe

I have a dataframe that contains Physician_Profile_City, Physician_Profile_State and Physician_Profile_Zip_Code. I ultimately want to stratify an analysis based on state, but unfortunately not all of the Physician_Profile_States are filled in. I started looking around to try and figure out how to fill in the missing States. I came across the pyzipcode module which can take as an input a zip code and returns the state as follows:
In [39]: from pyzipcode import ZipCodeDatabase
zcdb = ZipCodeDatabase()
zcdb = ZipCodeDatabase()
zipcode = zcdb[54115]
zipcode.state
Out[39]: u'WI'
What I'm struggling with is how I would iterate through the dataframe and add the appropriate "Physician_Profile_State" when that variable is missing. Any suggestions would be most appreciated.
No need to iterate if the form of the data is a dict then you should be able to perform the following:
df['Physician_Profile_State'] = df['Physician_Profile_Zip_Code'].map(zcdb)
Otherwise you can call apply like so:
df['Physician_Profile_State'] = df['Physician_Profile_Zip_Code'].apply(lambda x: zcdb[x].state)
In the case where the above won't work as it can't generate a Series to align with you df you can apply row-wise passing axis=1 to the df:
df['Physician_Profile_State'] = df[['Physician_Profile_Zip_Code']].apply(lambda x: zcdb[x].state, axis=1)
By using double square brackets we return a df allowing you to pass the axis param

Python Pandas: How to get column values to be a list?

I have a dataframe with one column and 20 rows. I want to use
dataframe[column].apply(lambda x : some_func(x))
to get second column. The function returns a list. Pandas is not giving me what I want. It is filling the second column with NaN instead of the list items that some_func() is returning.
Is there a clever or simple way to fix this?
It seems that the error was cause because I forgot to include:
axis = 1
My full line of code should have been:
dataframe[column].apply(lambda x : some_func(x), axis = 1)
You can just assign it like a dictionary:
dataframe['column2'] = dataframe['column1'].apply(lambda x : some_func(x))
Simple as that.

pix[x,y] from PIL produces name error

I am a beginner programmer and I am trying to access pixel data from a picture using python. I want to eventually get the pixel data into an array. I searched the web for the code of how to do this and this is what I got:
from PIL import Image
im = Image.open("C:/Users/Owner/Desktop/bw.png")
pix = im.load()
print pix[x,y]
pix[x,y] = value
It seems to work fine until I get to the print[x,y] line. I get an error saying "NameError: name 'x' is not defined". I have downloaded PIL 1.1.7.
Can anyone lend me a helping hand?
Uh, you didn't define x, y, or value...maybe try defining those first? What pixel did you want to access?
im = Image.open("C:/Users/Owner/Desktop/bw.png")
x, y = 1, 2 #sample coordinates
print im.getpixel((x, y))
that should work, note that to getpixel method you pass one argument - a tuple