Adarsh Maurya
Adarsh Maurya

Reputation: 358

Data Visulization : Matplotlib and Numpy throwing value error

I am new to machine learning. I was teaching myself data visualization with MATPLOTLIB. my code is pretty simple.

  1. It takes a numpy array (x = np.random.rand(1,100)) of shape=(1, 100)).
  2. It converts numpy array x into y(y = np.sin(x)).
  3. Final task is to visualise this in a BAR(plt.bar(x, y, label="BAR", color='r'))

But it is throwing VALUE ERROR.Even though there are already answers to this question, but none seems to work so far for me.

In one answer for this question By unutbu

he explains that this error is raised "whenever one tries to evaluate an array in boolean context". I am unable to understand how I am using these arrays as boolean?

MY CODE:

import matplotlib.pyplot as plt
import numpy as np

#arguments are shape: 1=row; 100=columns
x = np.random.rand(1, 100)
y = np.cos(x)

#bars
plt.bar(x, y, label='Bars1', color='pink')

#legends
plt.legend()

#show the figure
plt.show()

enter image description here

Upvotes: 2

Views: 76

Answers (1)

Miriam Farber
Miriam Farber

Reputation: 19634

You need to replace

x = np.random.rand(1, 100)

with

x = np.random.rand(100)

The reason is that the former gives you an array of arrays (with one array inside, but it is still a 2D array overall with dimensions 1-by-100), while the latter gives you a 1D array (of length 100). In order to visualize it with plt, you need the latter.

Upvotes: 3

Related Questions