Removing padding from Matplotlib spectrogram - matplotlib

I'm trying to remove the white spaces padding matplotlib's generated spectrogram. I've tried setting a limit to the axis and setting tight to the axis but these don't work.
Here's a screenshot:
Thanks

You can use plt.xlim() and plt.ylim() with the desired limits.
Your plt.axis(...) should have done, try to run it after the plot.
Similar question, and another.

Related

Turn off x-axis marginal distribution axes on jointplot using seaborn package

There is a similar question here, however I fail to adapt the provided solutions to my case.
I want to have a jointplot with kind=hex while removing the marginal plot of the x-axis as it contains no information. In the linked question the suggestion is to use JointGrid directly, however Seaborn then seems to to be unable to draw the hexbin plot.
joint_kws = dict(gridsize=70)
g = sns.jointplot(data=all_data, x="Minute of Hour", y="Frequency", kind="hex", joint_kws=joint_kws)
plt.ylim([49.9, 50.1])
plt.xlim([0, 60])
g.ax_joint.axvline(x=30,ymin=49, ymax=51)
plt.show()
plt.close()
How to remove the margin plot over the x-axis?
Why is the vertical line not drawn?
Also is there a way to exchange the right margin to a plot which more clearly resembles the density?
edit: Here is a sample of the dataset (33kB). Read it with pd.read_pickle("./data.pickle")
I've been fiddling with an analog problem (using a scatterplot instead of the hexbin). In the end, the solution to your first point is awkwardly simple. Just add this line :
g.ax_marg_x.remove()
Regarding your second point, I've no clue as to why no line is plotted. But a workaround seems to be to use vlines instead :
g.ax_joint.vlines(x=30, ymin=49, ymax=51)
Concerning your last point, I'm afraid I haven't understood it. If you mean increasing/reducing the margin between the subplots, you can use the space argument stated in the doc.

Hide part of an axis in a matplolib figure

How can I modulate the length of the yaxis of a matplotlib plot, as in the following figure:
In addition, how can I modulate the thickness of the yaxis?
I just found the answer. One need to use ax.spines['left'].set_bounds(4, -4).

Remove margins from a matplotlib figure

I'd like to plot a NumPy array using imshow in matplotlib and save it as a JPEG image. However, I can't manage to remove margins/paddings/borders from the image.
My code:
plt.imshow(np.arange(20).reshape(5,4)) ;
plt.axis('off')
plt.savefig('test.jpg', bbox_inches='tight', pad_inches=0, facecolor='black')
I've followed all recommendations that I could find here on Stackoverflow but none of them would help removing uneven white borders (I made them black in this figure) seen below:
setting pad_inches = -1 solved this for me (saving as png).
I suspect the pad_inches=0 was being interpreted as "falsey" and ignored
As it was described in this answer: https://stackoverflow.com/a/26610602/265289, it's important to also call:
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
alongside pad_inches=0. This removes the extra space to the left and bottom of the image.

How do I set the resize-behaviour of subplots?

I'm working with several subplots in a window. Just after plotting it looks great, problem is when I rezise the windows manually. The subplots do not adapt correctly to the new size of the window. How can I fix that?
Window after plotting, looking great:
window after resizing it manually, looks bad:
EDIT:
A simply demo-Code:
from matplotlib.pyplot import *
figure(figsize=(24,6))
subplot(131)
ylabel("test")
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")
subplot(132)
ylabel("test")
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")
subplot(133)
ylabel("test")
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")
tight_layout()
show()
As you see, the plot looks good after plotting it. But when you start to shrink the plot horizontally, the space between plot and plot gets smaller and smaller. And at the end, the ticklabels are on others plots, because there's no space for them. I need to know how to set that the entire plot gets smaller, leving space for the labels.
Maybe not exactly the answer of my question, but it solves my problem:
After creating the figure, you connect the resize-event to an eventhandler:
cid = fig.canvas.mpl_connect('resize_event', onresize)
def onresize(event):
plt.tight_layout()
As Wicket said, I'm just calling tight_layout() again and again, but automatically.

Matplotlib annotate doesn't work on log scale?

I am making log-log plots for different data sets and need to include the best fit line equation. I know where in the plot I should place the equation, but since the data sets have very different values, I'd like to use relative coordinates in the annotation. (Otherwise, the annotation would move for every data set.)
I am aware of the annotate() function of matplotlib, and I know that I can use textcoords='axes fraction' to enable relative coordinates. When I plot my data on the regular scale, it works. But then I change at least one of the scales to log and the annotation disappears. I get no error message.
Here's my code:
plt.clf()
samplevalues = [100,1000,5000,10^4]
ax = plt.subplot(111)
ax.plot(samplevalues,samplevalues,'o',color='black')
ax.annotate('hi',(0.5,0.5), textcoords='axes fraction')
ax.set_xscale('log')
ax.set_yscale('log')
plt.show()
If I comment out ax.set_xcale('log') and ax.set_ycale('log'), the annotation appears right in the middle of the plot (where it should be). Otherwise, it doesn't appear.
Thanks in advance for your help!
It may really be a bug as pointed out by #tcaswell in the comment but a workaround is to use text() in axis coords:
plt.clf()
samplevalues = [100,1000,5000,10^4]
ax = plt.subplot(111)
ax.loglog(samplevalues,samplevalues,'o',color='black')
ax.text(0.5, 0.5,'hi',transform=ax.transAxes)
plt.show()
Another approach is to use figtext() but that is more cumbersome to use if there are already several plots (panels).
By the way, in the code above, I plotted the data using log-log scale directly. That is, instead of:
ax.plot(samplevalues,samplevalues,'o',color='black')
ax.set_xscale('log')
ax.set_yscale('log')
I did:
ax.loglog(samplevalues,samplevalues,'o',color='black')