FAQ: Modules: Python - Modules Python Namespaces

This community-built FAQ covers the “Modules Python Namespaces” exercise from the lesson “Modules: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Modules Python Namespaces

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: What is the codecademylib3_seaborn module?

Why does pyplot display the graph at 90 degrees when the arguments are
reversed? I was expecting it backward.

What does the plt.plot(numbers_a, numbers_b) and the graph showed by executing plt.show() mean?? It seems like numbers_a (=range(1,13)) is the variables on the x axis and numbers_b (random.sample(range(1000), 12)) is the variables on the y axis but it is quite hard for me to figure out how this graph is made.

1 Like

Is the name used for aliasing a module_name called the namespace?

from matplotlib import pyplot as plt

Is “plt” the namespace here? I am facing difficulty in understanding what a namespace is.

As i understand it, the “namespace” is the set of “commands” that the python interpreter understands.
When you import a module the namespace gets expanded as you now have new, more numerous commands to use.

1 Like

You’ll get a better understanding if you read the documentation about namespace and scope. It says -

A namespace is a mapping from names to objects

these are implemented as python dictionaries. Namespace usually dictates which identifiers (for example class, variable, function, module etc.) can be used in a scope.
(This is what as far as i have understood :smiley: )

What exactly is a “namespace”?

The previous comment probably had the best place to start in the actual documentation-
https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces

Hi,

I tried to execute the same program on my Anaconda Spyder IDE, but the graph is not visible.
Below is the code and output.

#import codecademylib3_seaborn

# Add your code below:
from matplotlib import pyplot as plt

import random

numbers_a = range(1,12)

numbers_b = random.sample(range(1000), 12)
print(numbers_b)

plt.plot(numbers_a, numbers_b)
plt.show()

Output:

runfile('E:/Python_Modules/module2.py', wdir='E:/Python_Modules')
[147, 397, 949, 706, 795, 417, 410, 797, 908, 168, 16, 823]
Traceback (most recent call last):

  File "E:\Python_Modules\module2.py", line 16, in <module>
    plt.plot(numbers_a, numbers_b)

  File "C:\Users\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2796, in plot
    is not None else {}), **kwargs)

  File "C:\Users\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 1665, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]

  File "C:\Users\anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 225, in __call__
    yield from self._plot_args(this, kwargs)

  File "C:\Users\anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 391, in _plot_args
    x, y = self._xy_from_xy(x, y)

  File "C:\Users\anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 270, in _xy_from_xy
    "have shapes {} and {}".format(x.shape, y.shape))

ValueError: x and y must have same first dimension, but have shapes (11,) and (12,)
![image|380x252](upload://1x9iALEpByFqL9zzRHb7KuH8q5P.png) 

I am not sure, if this a right place to ask. But waiting for your right guidance and inputs.

Thanks.

As per the error code you are attempting to plot two sets of data together with unequal dimensions. numbers_a is 11 values long and and numbers_b is 12 values long. Since there’s no obvious way to plot this an error is thrown instead. You’d need to adjust your code to have equal dimensions for the data you’re passing to the plot function.

Thank you so much. How silly I was…
Thanks again for replying. :slightly_smiling_face:

If you’re not overly familar with numpy at a lot of those terms are a bit cryptic but they’re worth taking the time to learn now since they’re used so often. array.shape will quickly provide you with the relevant details of a numpy array (array size and dimensions) should you need to check them.

1 Like
import codecademylib3_seaborn
from matplotlib import pyplot as plt
import random
# Add your code below:
numbers_a = range(1, 13)
numbers_b = random.sample(range(1000), 12) plt.plot(numbers_a, numbers_b)
plt.show()

for some reason the graph isn’t showing even though i wrote the code right (i believe) and i wish to know why?

Is that how the code is written in your IDE on codecademy? Are you getting a syntax error for it? Check how the spacing for your logical lines is set-up.

when the arguments were reversed . The first arg would be on x axis and second arg would be on y axis. Hence you can understand from that and check the values manually you will understand it . Eg :[numbers_a = (range(1, 12)) (prints 12 numbers from 1 to 12)and numbers_b = random.sample(range(1000), 12) ](prints 12 numbers between 0 to 1000 ) .assume numbers_a = [1, 2 , 3, 4, 5](the increasing order of numbers here make the graph increasing from left to right or from bottom to top)(y axis) and numbers_b =[ 100, 300, 100, 200, 400](x axis).I have reduced the count of numbers so that understanding might be better.
Thank you :blush:

The instructions read

Create a variable numbers_b and set it equal to a random sample of twelve numbers within range(1000) .

but no matter what I do all the thing says is

Is number_b defined?

Can you post the code you’ve written so that we can see what might have gone wrong?

I got this far:

import codecademylib3_seaborn

# Add your code below:
from matplotlib import pyplot as plt

import random

numbers_a = range(1, 13)
numbers_b = random.sample(range(1000), 12)

and it kept bringing up a red error message at the bottom “Is number_b defined?”

The only way I could get past it was to use the “show me the solution” link

Not sure if you were able to resolve this, but it looks like it may have been a bug in the code that’s now fixed.

Your error message says, “Is number_b defined?”

But your code (as per the instructions) defines numberS_b, not number_b.

That may have been the problem.