Changing size parameters in seaborn without any success - seaborn

Here is the code:
import seaborn as sns
tips = sns.load_dataset("tips")
p1 = sns.countplot(x='sex',data=tips)
No matter what size adjustments I try to make by adding the height parameter or aspect, this is how it appears in Jupyter. Any idea how to change this? I am following a tutorial and in the video tutorial it appears fine, just not for me.

Related

Edit labels and move legend in seaborn [duplicate]

This question already has answers here:
Edit legend title and labels of Seaborn scatterplot and countplot
(3 answers)
How to edit a seaborn legend title and labels for figure-level functions
(2 answers)
How to customize seaborn.scatterplot legends?
(1 answer)
Closed 7 months ago.
I have a seaborn scatterplot, which I want to change its labels and move the legend outside the graph. There are many solutions to fix each of the issues, but I cannot fix both of them simultaneously. Adapted from a solution in stackoverflow which was intended for sns.lmplot:
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
g = sns.scatterplot(x="total_bill", y="tip", hue="smoker", data=tips, legend=False)
plt.legend(title='Smoker', bbox_to_anchor=(1.05, 1), labels=['Hell Yeh', 'Nah Bruh'])
plt.show(g)
Here legend=False and is defined later by matplotlib. The issue is the plot only displays the first label and not the rest in the legend.
I have tried another way to modify the label by legend='full' and later modifying it:
g = sns.scatterplot(x="total_bill", y="tip", hue="smoker", data=tips, legend='full')
h,l = g.get_legend_handles_labels()
l = ['Smoking behaviors:','Hell Yeh', 'Nah Bruh']
g.legend(h, l)
but the command g.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)does not work properly and edited labels do not show up. Only original labels are shown.
So, I can fix both of these issues separately, but not at the same time.
In the current version of Seaborn's scatterplot (0.11.1), you can first create the legend in full and afterwards change it by calling ax.legend() again.
Note that the return value of sns.scatterplot is an ax as it is an axes-level function. This should not be confused with figure level functions which return a complete grid of "axes" and often are written as g = sns....
Different seaborn functions create legends in different ways. Depending on the options used, the legend can become quite intricate and not easy to change. Making legends easier to modify is planned in Seaborn's future developments.
from matplotlib import pyplot as plt
import seaborn as sns
tips = sns.load_dataset('tips')
ax = sns.scatterplot(x="total_bill", y="tip", hue="smoker", data=tips, legend='full')
ax.legend(title='Smoker', bbox_to_anchor=(1.05, 1), labels=['Hell Yeh', 'Nah Bruh'])
plt.tight_layout()
plt.show()

Handling matplotlib.figure.Figure using openCV

I am using the following code to find the spectrogram of a signal and save it.
spec,freq,t,im = plt.specgram(raw_signal,Fs=100,NFFT=100,noverlap=50)
plt.axis('off')
figure = plt.gcf()
figure.set_size_inches(12, 1)
plt.savefig('spectrogram',bbox_inches = 'tight',pad_inches=0)
But I have multiple spectrograms like this and the end product I need is a concatenation of all these. Right now, what I am doing is, I am saving all these individual images using plt.savefig() as earlier and reading them back using cv2.imread() and concatenating them. But this process is not very good I think. So is there any other way I can do this without saving it and re-reading it?
One possible idea I have is, somehow converting matplotlib.figure.Figure into a format that can be handled by OpenCV (specifically cv2). However, it should also not have white padding.
You can get the image as an array using buffer_rgba (don't forget to draw the image first). Then in OpenCV, you need to convert the image from RGB to OpenCV's BGR channel ordering.
import matplotlib.pyplot as plt
import numpy as np
import cv2
raw_signal = np.random.random(1000)
spec,freq,t,im = plt.specgram(raw_signal,Fs=100,NFFT=100,noverlap=50)
plt.axis('off')
figure = plt.gcf()
figure.set_size_inches(12, 1)
figure.set_dpi(50)
figure.canvas.draw()
b = figure.axes[0].get_window_extent()
img = np.array(figure.canvas.buffer_rgba())
img = img[int(b.y0):int(b.y1),int(b.x0):int(b.x1),:]
img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGRA)
cv2.imshow('OpenCV',img)
Top: matplotlib, bottom OpenCV:
don't save the figure. matplotlib happens to have a convenience function for displaying time series data in this way but that's not how you deal with spectrograms. any handling of spectrogram "pictures" is a kludge.
use scipy.signal.spectrogram to get the actual spectrogram.

How to remove watermark background in image Python

I have a image like below,
I would like to remove background watermark.
So far I tried, inpainting method in opencv. It didn't help me.
I tried following script:
edges = cv2.Canny(img,50,150,apertureSize = 3)
dst = cv2.inpaint(img,edges,3,cv2.INPAINT_TELEA)
I am new to image processing and opencv. So, I don't know whether I'm doing in the correct way or not for performing inpainting. What method should I do for removing background watermarks.
I would like to remove green quoted watermark from my image.
any help would be more appreciable.
Text here has a different intensity than the watermark. You could play around with a simple brightness/contrast transformation, i.e. increasing gain/contrast until the watermark vanishes and reducing brightness to compensate.
See OpenCV docs for a simple tutorial.
Here's a quick attempt in Python, not really using OpenCV because it's not needed IMHO for such a simple linear transformation. Play around with alpha (contrast) and beta (brightness) parameters until you get the result you want
import cv2
import numpy as np
img = cv2.imread("veidz.jpg")
alpha = 2.0
beta = -160
new = alpha * img + beta
new = np.clip(new, 0, 255).astype(np.uint8)
cv2.imwrite("cleaned.png", new)

Pygal.maps.world.World() is not interactive

I am trying to build an interactive Pygal worldmap. I posted my code below, but any ideas why the numbers are not showing up when I hover over the map? Thanks!
import pygal
wm = pygal.maps.world.World()
wm.title = "Americas"
wm.add('North America',{'ca': 320161819,'mx': 115405161,'us': 301018101})
wm.render_to_file('americas.svg')

Healpy Mollview function returned nothing

I am a new healpy user. I used healpy tutorial available at page http://healpy.readthedocs.org/en/latest/tutorial.html for creating a map but after execution of "healpy.mollview" command it returned nothing and no plot was visible. Need Help!
I have searched the problem already but unable to find the exact situation anywhere
Thanks,
Jibran
For using healpy plotting functions, the best is to use ipython, in particular:
ipython --pylab
You have to show the plot
import matplotlib.pyplot as plt
plt.show()
as you would do for any plot in matplotlib

Resources