This question already has answers here:
Numpy binary matrix - get rows and columns of True elements
(1 answer)
Python: How to find the value of a number in a numpy array?
(1 answer)
Closed 5 years ago.
I have a grayscale image and I want to get all the coordinates of pixels with intensity level 255.
I tried using:
pixels = np.where(img[img == 255])
But instead of getting a list of (X,Y) coordinates, I got a list of numbers, like if img was a one dimensional vector.
What am I doing wrong?
Related
This question already has answers here:
Seaborn kde plot plotting probabilities instead of density (histplot without bars)
(1 answer)
Seaborn distplot y-axis normalisation wrong ticklabels
(1 answer)
How to normalize seaborn distplot?
(2 answers)
Why does the y-axis of my distplot contain values up to 150?
(1 answer)
Closed 17 hours ago.
I'd like to construct a univariate KDE plot using Seaborn. The x-axis is amygdala volume, and I would like the y-axis to display probability densities. But when I use seaborn's kdeplot method, it seems like it's returning the raw counts (as if it was a histogram) instead of the densities. How do I get it to show the densities?
I've tried looking for parameters to include, but nothing would help.
plt.figure(figsize=(15,8))
sns.kdeplot(data=n90pol, x='amygdala', bw_adjust=0.5)
plt.xlabel('Amygdala Volume', fontsize=16);
plt.ylabel('Density', fontsize=16);
plt.title('KDE plot of Amygdala Volume', fontsize=24)
This question already has answers here:
How to create a bar chart/histogram with bar per discrete value?
(1 answer)
Matplotlib xticks not lining up with histogram
(5 answers)
Matplotlib: incorrect histograms
(1 answer)
Python Matplotlib pyplot histogram
(3 answers)
Closed 4 months ago.
I am making a histogram using matplotlib. I am using integer data and I want them to represent 1 bin. The following is the sample code which I made.
import matplotlib.pyplot as plt
a=[1,2,2,3,3,3,4,4,4,4]
plt.figure()
plt.hist(a,bins=4)
plt.show()
The histogram which I got is above. This have left end 1 ad rightend 4. I want the width of the histogram to be 1, however, this will just show 0.75 size. Moreover, I want the x value to locate at the center of the bar of histogram. Is there any way I can adjust?
This question already has answers here:
How to set Matplotlib colorbar height for image with aspect ratio < 1
(2 answers)
Set Matplotlib colorbar size to match graph
(9 answers)
Setting the size of a matplotlib ColorbarBase object
(2 answers)
Closed 11 months ago.
There are a few examples showing how to use "shrink" for changing the colorbar. How can I automatically figure out what the shrink should be so the colorbar is equal to the height of the heatmap?
I don't have a matplotlib axis because I am using seaborn and plotting the heatmap from the dataframe.
r = []
r.append(np.arange(0,1,.1))
r.append(np.arange(0,1,.1))
r.append(np.arange(0,1,.1))
df_cm = pd.DataFrame(r)
sns.heatmap(df_cm, square=True, cbar_kws=dict(ticks=[df_cm.min().min(), df_cm.max().max()]))
plt.tight_layout()
plt.savefig(f'test.png', bbox_inches="tight")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm having trouble converting a tuple containing the coordinates of polygon vertices to a shapefile.
Tuples are a very unfamiliar format to me; if it were in a dataframe, I could do it easily with geopandas.
shape= ({'type': 'Polygon',
'coordinates': [[(-148.7285301097261, 60.42704276401832),
(-148.7285301097261, 60.42693172262919),
(-148.7285856304207, 60.42693172262919),
(-148.72830802694787, 60.42704276401832),
(-148.7285301097261, 60.42704276401832)]]},
1.0)
I can't convert to dataframe via pd.DataFrame(shape); can't subset the tuple to access coordinates via shape['coordinates'] or pd.DataFrame(list(shape)). I've reviewed this, and this, but am stuck on getting the coordinates out of the Tuple structure!
How can I create a shapefile (via Geopandas), given a tuple of the structure shown here?
You should be able to convert it to pandas DataFrame by reading the first element of the tuple:
pd.DataFrame(shape[0]).explode('coordinates')
Out[1]:
type coordinates
0 Polygon (-148.7285301097261, 60.42704276401832)
0 Polygon (-148.7285301097261, 60.42693172262919)
0 Polygon (-148.7285856304207, 60.42693172262919)
0 Polygon (-148.72830802694787, 60.42704276401832)
0 Polygon (-148.7285301097261, 60.42704276401832)
If you need to split into x and y you can just take the items from the series:
df = pd.DataFrame(shape[0]).explode('coordinates').reset_index(drop=True)
df = df.join(df['coordinates'].apply(pd.Series)).rename(columns={0:'x', 1:'y'}).drop('coordinates', axis=1)
Out[2]:
type x y
0 Polygon -148.728530 60.427043
1 Polygon -148.728530 60.426932
2 Polygon -148.728586 60.426932
3 Polygon -148.728308 60.427043
4 Polygon -148.728530 60.427043
This question already has answers here:
Pygame, create grayscale from 2d numpy array
(4 answers)
How do I convert an OpenCV image (BGR and BGRA) to a pygame.Surface object
(1 answer)
Closed 1 year ago.
I am using pygame to show grayscale images. However, it can't show grayscale image, the output is here
The images are numpy arrays of which shape is 80X80, and the code is here
surf_1 = pygame.surfarray.make_surface(pic_1)
surf_2 = pygame.surfarray.make_surface(pic_2)
surf_3 = pygame.surfarray.make_surface(pic_3)
surf_4 = pygame.surfarray.make_surface(pic_4)
self.screen.blit(surf_1, (120, 40))
self.screen.blit(surf_2, (210, 40))
self.screen.blit(surf_3, (300, 40))
self.screen.blit(surf_4, (390, 40))
My question is how can I show real grayscale images instead of the blue-green one.
Update
Here is the code to convert RGB to grayscale, it is from the Internet, I don't know the exact source, sorry about that.
def rgb2gray(rgb, img_shape):
gray = np.dot(rgb[..., :3], [0.299, 0.587, 0.114])
return gray.reshape(*img_shape)