PyPlot: hide axes but keep axis labels - matplotlib

I have the following code:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 4)
for n, ax in enumerate(axs):
ax.plot([1, 2], [1, 2])
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlabel(n)
plt.show()
...which displays this:
What I want is to hide the black boxes but keep the labels. I've tried adding ax.set_axis_off() but that removes the labels as well:
How can I do this?

Just change the color of spines to None:
fig, axs = plt.subplots(1, 4)
for n, ax in enumerate(axs):
ax.plot([1, 2], [1, 2])
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlabel(n)
plt.setp(ax.spines.values(), color=None)
plt.show()

Related

In matplotlib, how to set ylim independently while setting sharey=True

I want to replicate seaborn pairplot function but I need increased flexibility to plot extra things.
I have the following minimal example, but I need that the uppermost labels show the range [10, 20] instead of [0, 1] without changing the displayed range of the first plot, and that the grid for the right plots shows horizontal bars.
How can I keep x and y shared and displayed for all plots but exclude the y axis of diagonal plots as in seaborn?
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=2, sharex='col',
sharey='row')
axes[0, 0]._shared_axes['y'].remove(axes[0, 0])
axes[1, 1]._shared_axes['y'].remove(axes[1, 1])
axes[0, 0].plot([0, 1])
axes[0, 1].scatter([0, 1], [10, 20])
axes[1, 0].scatter([0, 1], [100, 200])
axes[1, 1].plot([0, 1])
axes[0, 0].grid(color='lightgray', linestyle='--')
axes[0, 1].grid(color='lightgray', linestyle='--')
axes[1, 0].grid(color='lightgray', linestyle='--')
axes[1, 1].grid(color='lightgray', linestyle='--')
plt.show()
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.DataFrame([[10, 100], [20, 200]])
sns.pairplot(df, diag_kind='kde')
plt.show()

How to highlight selected panels in matplotlib? Tight layout ignored added patched

I want to highlight some panels of my figure, but the command tight_layout() will misalign the added rectangle with the aimed panel.
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
fig, axs = plt.subplots(3, 3, figsize=(5, 5), sharex=True, sharey=True,
gridspec_kw={'hspace': 0, 'wspace': 0})
axs = axs.flatten()
ax = axs[4]
chartBox = ax.get_position()
x0, y0, x1, y1, w, h = chartBox.x0, chartBox.y0, chartBox.x1, chartBox.y1, chartBox.width, chartBox.height
fig.patches.extend([plt.Rectangle((x0, y0),
w, h,
fc ='none',
transform=fig.transFigure,
figure=fig,
ec ='r',
lw = 4)])
plt.tight_layout()
plt.show()
mport matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
fig, axs = plt.subplots(3, 3, figsize=(5, 5), sharex=True, sharey=True,
gridspec_kw={'hspace': 0, 'wspace': 0})
ax = axs[1, 1]
fig.patches.extend([plt.Rectangle((0, 0),
1, 1,
fc ='none',
transform=ax.transAxes,
figure=fig,
ec ='r',
lw = 4)])
plt.show()
Gives you exactly what you want.
One of the solutions with minimum change of the above code is to provoke tight_layout() earlier than plotting the patch.
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
fig, axs = plt.subplots(3, 3, figsize=(5, 5), sharex=True, sharey=True,
gridspec_kw={'hspace': 0, 'wspace': 0})
axs = axs.flatten()
plt.tight_layout()
ax = axs[4]
chartBox = ax.get_position()
x0, y0, x1, y1, w, h = chartBox.x0, chartBox.y0, chartBox.x1, chartBox.y1, chartBox.width, chartBox.height
fig.patches.extend([plt.Rectangle((x0, y0),
w, h,
fc ='none',
transform=fig.transFigure,
figure=fig,
ec ='r',
lw = 4)])
plt.show()

Matplotlib inconvenient "reset to original view" while previously zoomed

Updated:
After having zoomed to a region of interest, I would like to add a scatter point without having a reset to the original view.
It occurs when I double click after having zoomed to rectangle.
Of course this is a simplification of a problem encountered as I wanted to add markers to a large image after having properly zoomed to a region of interest.
Any help welcomed
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, figsize=(4,4))
plt.plot([0, 1, 2, 3], [10, 20, 30, 40])
def onclick(event):
if event.dblclick:
plt.scatter(event.xdata, event.ydata, c='r')
fig.canvas.mpl_connect('button_press_event', onclick)
plt.get_current_fig_manager().toolbar.zoom()
plt.show()
Answer:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, figsize=(4,4))
ax.imshow([[1, 2], [5, 6]])
ax.autoscale(False) # disable autoscaling for all future plotting functions.
def onclick(event):
if event.dblclick:
plt.scatter(event.xdata, event.ydata, c='r')
fig.canvas.mpl_connect('button_press_event', onclick)
plt.get_current_fig_manager().toolbar.zoom()
plt.show()
I believe the problem comes from the autoscale features that kicks in whenever you call plt.scatter(). The solution is simply to disable autoscale (but draw the initial plot beforehand):
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, figsize=(4, 4))
plt.plot([0, 1, 2, 3], [10, 20, 30, 40])
fig.canvas.draw() # force draw so that the axes are autoscaled here
ax.autoscale(False) # disable autoscaling for all future plotting functions.
def onclick(event):
if event.inaxes and event.dblclick:
plt.scatter(event.xdata, event.ydata, marker='o', s=10, c='r')
fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()

Label is Missing from matplotlib legend

I'm plotting subplots with matplotlib and the legend does not show up for some plots.
In this example, the scatter plot legend does not show up.
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
from matplotlib.legend_handler import HandlerLine2D
from matplotlib.patches import Rectangle, Circle
fig = plt.figure()
plt.cla()
plt.clf()
x = np.arange(5) + 1
y = np.full(5, 10)
fig, subplots = plt.subplots(2, sharex=False, sharey=False)
subplots[0].bar(x, y, color='r', alpha=0.5, label='a')
scat = subplots[0].scatter(x, y-1, color='g', label='c')
subplots[0].set_yscale('log')
subplots[1].bar(x, y, color='r', alpha=0.5, label='a')
x = [2, 3]
y = [4, 4]
subplots[1].bar(x, y, color='b', alpha=1, label='b')
subplots[1].set_yscale('log')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), handler_map={scat: HandlerLine2D(numpoints=4)})
plt.show()
Here is what I tried as a workaround:
p1 = Rectangle((0, 0), 1, 1, fc="r", alpha=0.5)
p2 = Rectangle((0, 0), 1, 1, fc="b")
p3 = Circle((0, 0), 1, fc="g")
legend([p1, p2, p3], ['a', 'b', 'c'], loc='center left', bbox_to_anchor=(1, 0.5))
I really prefer to fix this without the workaround so if anyone knows how to fix it please let me know.
Also, an issue with the workaround is that the Circle object still appears as a bar on the legend.
plt.legend starts with a gca() (which returns the current axes):
# from pyplot.py:
def legend(*args, **kwargs):
ret = gca().legend(*args, **kwargs)
So calling plt.legend will only get you a legend on your last subplot. But it is also possible to call e.g. ax.legend(), or in your case subplots[0].legend(). Adding that to the end of your code gives me a legend for both subplots.
Sample:
for subplot in subplots:
subplot.legend(loc='center left', bbox_to_anchor=(1, 0.5))

Scatter plot in matplotlib not updating xlim and ylim

Given the below code I would expect the x-axis to be between 0 and 3 with some margins added.
Instead it is much larger. I would expect the call to scatter to automatically update x-axis limits.
I could set the xlim and ylim my self but would like them to be set automatically. What am I doing wrong?
import matplotlib.pyplot as plt
if __name__ == '__main__':
fig = plt.figure()
ax = fig.add_subplot(111)
x = [0, 4000, 8000, 100000]
y = [0, 10, 100, 150]
ax.scatter(x, y)
x = [0, 1, 2, 3]
y = x
ax.clear()
ax.scatter(x, y)
plt.show()
You can clear the figure, and open a new subplot, than the axes will be adjusted as you wanted.
fig = plt.figure()
ax = fig.add_subplot(111)
x = [0, 4000, 8000, 100000]
y = [0, 10, 100, 150]
ax.scatter(x, y)
plt.clf()
ax = fig.add_subplot(111)
x = [0, 1, 2, 3]
y = x
ax.scatter(x, y)
plt.show()
Edit: In this version figure is not closed, just cleared with the clf function.
It is a feature that scatter does not automatically re-limit the graph as in many cases that would be undesirable. See Axes.autoscale and Axes.relim
ax.relim() # might not be needed
ax.autoscale()
should do what you want.
Here is a way of making another scatter plot without needing to clear the figure.
I basically update offests of PathCollection returned by axes.scatter() and add the collection back to the axes. Of course, the axes has to be cleared first. One thing I notice is that I have to manually set the margins.
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
x = [0, 4000, 8000, 100000]
y = [0, 10, 100, 150]
scat = ax.scatter(x, y)
x = [0, 1, 2, 3]
y = x
ax.clear()
corners = (min(x), min(y)), (max(x), max(y))
ax.update_datalim(corners)
ax.margins(0.05, 0.05)
ax.autoscale_view()
scat.set_offsets(np.vstack((x,y)).transpose())
ax.add_collection(scat)
plt.show()
If you comment out the first scatter, everything will be fine