How to remove ylabel from Seaborn histplot? [duplicate] - matplotlib

This question already has an answer here:
How to remove or hide y-axis ticklabels from a matplotlib / seaborn plot
(1 answer)
Closed 5 months ago.
This is what I have:
fig, ax = plt.subplots(figsize=(4, 3))
sns.histplot(my_data, ax=ax)
ax.set(ylabel='')
But this still seems to allocate the space for the y-axis label, it's only that the label itself is an empty string, which results in wasted space on the left-hand side of the image. I don't want the white space in place of the y-axis label, I really want to remove it.

Both answers suggested in the comments work.
One way to achieve what I want is:
fig, ax = plt.subplots(figsize=(4, 3))
sns.histplot(my_data, ax=ax)
ax.set(ylabel='')
plt.tight_layout(pad=0)
The pad parameter of tight_layout controls the size of the margin around the figure.

Related

Adjust position of colorbar in Matplotlib subplots [duplicate]

This question already has answers here:
Python matplotlib - how to move colorbar without resizing the heatmap?
(1 answer)
Matplotlib: let color bar not affect size and proportions of the plot
(2 answers)
How do I maintain image size when using a colorbar?
(2 answers)
Adjusting subplots to make space for colorbar
(1 answer)
Closed last month.
The Plot I Want to Fix
I am trying to graph three graphs stacked on top of each other and for the most part it works fine.
However when I add a colorbar to the last spectogram plot it completely squishes the plot, making the overall figure ugly.... How do I fix this?
fig, ax = plt.subplots(3, sharex=True, figsize=(50, 10))
fig.suptitle('Test' + str("%03d" % (i,)) + '+ Noisereduce', fontsize=48)
ax[0].plot(time, raw, color='blue')
ax[0].plot(time, fltr, color='orange')
ax[0].set_title('Raw Signal')
ax[1].plot(time, fltr, color='orange')
ax[1].set_title('noisereduce (stationary filter)')
spect = ax[2].pcolormesh(t, f, 10*np.log10(Sxx), vmin=vmin, vmax=vmax, shading='gouraud')
ax[2].set(xlabel='Time [sec]', ylabel='Frequency [Hz]')
fig.colorbar(spect, ax=ax[2])
ax[2].set_title('noisereduce - spectogram (upper 67% of intensities)')
plt.show()

How can I add an arbitrarily big white margin to a figure with subplots?

I am trying to add an arbitrarily big white margin (or padding) to a figure with subplots because I would like the subtitle of the figure not to overlap with any of the subplots or titles of these subplots. I am using Matplotlib 3.1.2.
Currently, I have the following source code.
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(2, 1, figsize=(15, 10))
n = 10
x = np.arange(0, n)
y = np.random.rand(n)
ax[0].plot(x, y)
ax[0].set_xlabel('x')
ax[0].set_ylabel('y')
y = np.random.rand(n)
ax[1].plot(x, y)
ax[1].set_xlabel('x')
ax[1].set_ylabel('y')
fig.suptitle("I want to have white space around me!!!")
# fig.tight_layout(rect=[0, 0.03, 1, 0.80])
plt.subplots_adjust(top=0.85)
plt.show()
If I try to use either tight_layout or subplots_adjust (as suggested in several answers to this question Matplotlib tight_layout() doesn't take into account figure suptitle), it doesn't seem to have any effect on the margins. Here's the result of the execution of the previous example.
Is there a way to add an arbitrarily big white margin to the left, right, bottom and (or) top of a figure (with subplots)? I would like to specify the figure size and arbitrarily increase or decrease the white space around an image. I also would like the solution to work in case I decide to add a title for each of the subplots. How can this be done?
fig, axs = plt.subplots(2,1, figsize=(5,5))
fig.patch.set_facecolor('grey')
fig.suptitle("Look at all that grey space around me!!!")
fig.subplots_adjust(top=0.6, bottom=0.4, left=0.4, right=0.6)

How to remove all padding in matplotlib subplots when using images [duplicate]

This question already has answers here:
How to combine gridspec with plt.subplots() to eliminate space between rows of subplots
(1 answer)
How to remove the space between subplots in matplotlib.pyplot?
(5 answers)
Closed 3 years ago.
When creating subplots with matplotlib i cannot get tight layout where there would not be any spaces between subplot items.
import numpy as np
import matplotlib.pyplot as plt
fig, axes = plt.subplots(3, 3, figsize=(10,10),gridspec_kw = {'wspace':0, 'hspace':0})
for i, ax in enumerate(axes.ravel()):
im = ax.imshow(np.random.normal(size=200).reshape([10,20]))
ax.axis('off')
plt.tight_layout()
Subplots would consist of images. Seems like there is way to do this when you are not using images. So i assume, there is some configuration about imshow().
I would like to keep aspect ratio of images, but make subplots compact as possible.
this is what i get now, but as you can see, there is a lot of row padding
https://imgur.com/a/u4IntRV

Matplotlib: How to plot an empty circle in an scatter plot using pandas plot api? [duplicate]

This question already has answers here:
How to do a scatter plot with empty circles in Python?
(6 answers)
Closed 4 years ago.
I'm trying to plot a scatter plot with pandas api where each point is an empty circle, just with border color and transparency. I've tried a lot of tweaks in this code:
ax = ddf.plot.scatter(
x='espvida',
y='e_anosestudo',
c=ddf['cor'],
alpha=.2,
marker='o');
The generated plot looks like this:
If you look closely at the points:
you'll see that they have a transparent fill color and a border. I'd like it to have just a transparent border. Hou would I do it?
I can't seem to get it to work with DataFrame.plot.scatter; it doesn't seem to respect the facecolors='none' kwarg, likely because some default color argument is being passed to plt.scatter.
Instead, fall back to matplotlib, specifying facecolors='none' and setting the edgecolors to the column in your df that represents the color.
Sample Data
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({'x': np.random.normal(1,1,1000),
'y': np.random.normal(1,1,1000),
'color': list('rgby')*250})
plt.scatter(df.x.values, df.y.values, facecolors='none', edgecolors=df['color'], alpha=0.2, s=100)
plt.show()
From the matplotlib scatter doc:
edgecolors : color or sequence of color, optional, default: 'face'. The edge color of the marker. Possible values:
'face': The edge color will always be the same as the face color.
'none': No patch boundary will be drawn.
A matplotib color.
For non-filled markers, the edgecolors kwarg is ignored and forced to 'face' internally
Try add: edgecolors='none':
ax = ddf.plot.scatter(
x='espvida',
y='e_anosestudo',
c=ddf['cor'],
alpha=.2,
marker='o',
edgecolors='none);

Change colour of curve according to its y-value in matplotlib [duplicate]

This question already has answers here:
Having line color vary with data index for line graph in matplotlib?
(4 answers)
Set line colors according to colormap
(1 answer)
Closed 8 years ago.
I'm trying to replicate the style of the attached figure using matplotlib's facilities.
Basically, I want to change the colour of the curve according to its y-value using matplotlib.
The plot you've shown doesn't have the color set by the vertical axis of the plot (which is what I would consider the y-value). Instead, it just has 8 different plots overlain, each with a different color, without stating what the color means.
Here's an example of something that looks like your plot:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
# some fake data:
x = np.linspace(0, 2*np.pi, 1000)
fs = np.arange(1, 5.)
ys = np.sin(x*fs[:, None])
for y, f in zip(ys, fs):
plt.plot(x, y, lw=3, c=cm.hot(f/5))
If you actually want the color of one line to change with respect to its value, you have to kind of hack it, because any given Line2D object can only have one color, as far as I know. One way to do this is to make a scatter plot, where each dot can have any color.
x = np.linspace(0, 2*np.pi, 1000)
y = np.sin(2*x)
plt.scatter(x,y, c=cm.hot(np.abs(y)), edgecolor='none')
Notes:
The color vector should range between 0 and 1, so if y.max() > 1, then normalize by it: c=cm.hot(y/y.max()) and make sure it's all positive.
I used edgecolor='none' because by default the scatter markers have a black outline which makes the it look less like a uniform line.
If your data is spaced too far, you'll have to interpolate the data if you don't want gaps between markers.