-1

I tried plotting individual axes on my system with the basic code

import matplotlib.pyplot as plt
import numpy as np
x=[1,2,3,4,5]
y=[2,4,6,8,10]



fig,ax=plt.subplots(2)
ax[0].plot(x,y)
ax[1].plot(x,[2*i for i in y])

plt.show()

Is there a way to display individual axes from a figure while running matplotlib in a python interpreter from terminal?

  • This is intended behavior. Use `plt.show()` in scripts and don't use it in Jupyter Notebooks. – paime Aug 24 '23 at 10:50
  • ok, can you tell the reason why is this so and is there any other way to display just one axes. Because why display all the axes in a figure when I'm making change to a single axes. – Sahil Ahmed Aug 24 '23 at 11:01
  • 1
    Can you clarify the question in your last comment? Where are you asking for any other way? In Jupyter or Python run from the command line? ... To extend what paime said and maybe address the first part of your last comment: `plt.show()` is no longer necessary in modern Jupyter because the Jupyter Python machinery monitors if a matplotlib figure object is being made and displays it if one is created in the cell. You can indeed still have `plt.show()` at the end of the cell in Jupyter to keep the code more consistent between the two places you run the code. – Wayne Aug 24 '23 at 12:23
  • I was asking why when I run the first snippet of code in my question in Python run from the command line it doesn't show any graph, which from your comment I infer that is because Python doesn't monitors if a matplotlib figure object is being made and so doesn't displays it if one is created . And I want to know about a way to display individual axes when run from python in the command line. – Sahil Ahmed Aug 24 '23 at 13:41
  • If you are running in an interactive environment, the plot is shown automatically and `plt.show()` isn't needed. This makes things easier for new users. Inside a large program, however, you usually want to control the exact moment a plot gets shown to the user. The exact behavior depends on the environment where you run the code. The plot isn't shown at the moment the figure is created, but at the end, when everything is added. Otherwise the plot would be redrawn and constantly flickering. – JohanC Aug 24 '23 at 13:41
  • So if i'm using the python interpreter to run the code, and I made changes to one of the axes, then i'll have to use `plt.show()` , which effectively will show the entire figure with all the axes and there is no function within the plt object to display individual axes of a figure? – Sahil Ahmed Aug 24 '23 at 13:48

0 Answers0