Unable to view plots from matplotlib in output tab

I am using python library matplotlib to plot 2 line graphs, however only 1 line graph is displayed in the output tab. Why is that so? Code below:

import random
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

X = [i for i in range(20)] # list comprehension style
Y = [2*X[i] for i in range(20)]

plt.title('Y=2X')
plt.xlabel('X values')
plt.ylabel('Y Values')

plt.plot(X, Y, marker='o',
     markerfacecolor='blue', markersize=3)

plt.show()  # here it will display. 

# for the same X, Y values show a different style

plt.title('Y=2X')
plt.xlabel('X values')
plt.ylabel('Y Values')
plt.plot(X, Y, linestyle = 'dashed',marker='o',
     markerfacecolor='green', markersize=3)

plt.show()

Your code shows graphs not simultaneously, as you might think, but alternately. That is, the second chart will appear only when you close the first one. To do this, click on the cross button in the upper right corner of the chart. The same thing would happen if you were running your code locally on a computer.

1 Like