I have several plt.plot
instances and I wanted to only plt.show()
certain objects. To illustrate here is some code:
import matplotlib.pyplot as plt
ax1 = plt.plot(range(5),range(5))
ax2 = plt.plot([x+1 for x in range(5)],[x+1 for x in range(5)])
ax3 = plt.plot([x+2 for x in range(5)],[x+2 for x in range(5)])
#plt.show([ax1,ax2])
plt.show()
So I would like something like the commented out statement, to display only ax1 & ax2 in the example figure.
Using plt. show() in Matplotlib mode is not required.
ion() in Python. Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.
As noted above, there are essentially two ways to use Matplotlib: Explicitly create Figures and Axes, and call methods on them (the "object-oriented (OO) style"). Rely on pyplot to automatically create and manage the Figures and Axes, and use pyplot functions for plotting.
You can remove some of the plotted lines from the set of lines of the current axes:
axes = plt.gca() # Get current axes
axes.lines.remove(ax2[0]) # Removes the (first and only) line created in ax2
plt.draw() # Updates the graph (in interactive mode)
If you want to put it back, you can similarly do
axes.lines.append(ax2[0]) # Puts the line back (the drawing order is changed, here)
You could alternatively save the current graph lines, if you need to put them back later:
all_lines = list(axes.lines) # Copy
# ...
axes.lines[:] = all_lines # All lines put back
The key point is that each plot()
command adds a line to the current axes and draws it (in interactive mode). So you can either remove already plotted lines (like in this answer).
As Yann pointed out, you can also make some lines invisible. However, the method from this answer is probably faster, since there are fewer lines to be drawn (if this matters).
Not exactly. First off, the plt.plot
call does not return an axes, it returns a list of Line2D objects, one for each line plotted. You can use the OO interface to Matplotlib to create a separate axes for each plot, then selectively add those as subplots, etc. There are a lot of different ways to selectively reveal a plot.
But for your example, you can take advantage of the Line2D's alpha value, ie how opaque it is, to make any one line invisible. Here's a modified version of your example:
import matplotlib.pyplot as plt
line1 = plt.plot(range(5),range(5))
line2 = plt.plot([x+1 for x in range(5)],[x+1 for x in range(5)])
line3 = plt.plot([x+2 for x in range(5)],[x+2 for x in range(5)])
print line3, " see I'm a list of lines"
print line3[0].get_alpha()
line3[0].set_alpha(0) # make complete opaque
#plt.show([ax1,ax2])
plt.gcf().savefig('line3opaque.png')
line3[0].set_alpha(1) # make visible
line1[0].set_alpha(0) # make opaque
plt.gcf().savefig('line1opaque.png')
plt.show()
The first figure I saved is 'line3opaque.png'; this is what I get:
Line 3 is not there and lines 1 and 2 are. For 'line1opaque.png' I get:
Now we have line 1 missing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With