Adarsh Jaiswal
Adarsh Jaiswal

Reputation: 1

Why the graph is empty

I have written the below code in Google Colab for plotting a graph but apparently it printed the blank graph. Can someone please explain why?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8,9,10]

y = np.arange(0,len(x))

for i in x:
  plt.plot(i,y[i-1])

Result:

Empty Graph Result

Upvotes: 0

Views: 500

Answers (1)

gboffi
gboffi

Reputation: 25023

Try for i in x: plt.plot(i,y[i-1], '-D') (i.e., place a Diamond symbol in every point)

enter image description here

and ask yourself,

  1. why the diamonds have different colors? and
  2. what is the meaning of, e.g., plt.plot(2,2)?

It seems that Matplotlib , when it sees plt.plot(x, y) with x and y both scalar values, draws a line of ZERO LENGTH from P(x, y) to P(x, y) and the only reasonable way to to have no empty graph is to add a symbol to each point in each line.

I wonder if Matplotlib draws a single Diamond for each individual, zero length line or it's drawing TWO Diamonds, one on the first point, P(x, y) and another one on the final point P(x, y) … :-)

Upvotes: 1

Related Questions