Dictionary query

Screen Link: Learn data science with Python and R projects

We have been taught to use for loop in a dictionary. But this is something not comprehensible. Can you please help? What is the work going behind?

for key,spine in ax.spines.items():
spine.set_visible(False)

Hi @keswani06,

Good question! When I can’t figure out what a piece of code is doing what I like to do is try commenting that piece of code out and see if it still works. In this case you can comment the for loop out and we see output that looks like the following image.

When we compare this image to the solution we notice that when we exclude this for loop from our code we get borders around each of the charts. These borders are what matplotlib calls spines.

Now I know what you are thinking… “but that doesn’t answer my questions!” That’s true, but it does help give us some clues into what is going on with this code.

To explain in more detail this bit of code is accessing matplolib objects. In other words, it is using functionarlity that matplotlib provides to us when making graphs. Here we are using the ax object which represents the portion of the figure where the graph is drawn. This object has a method on it (a method is like a function that allows us to perform certain tasks) called spines which represents the borders in the figure/chart.

So what our loop is telling matplotlib to do is don’t draw the borders around this chart.

Another helpful way to figure out what is going on with code we don’t understand is to use print(). I don’t have matplotlib memorized and I didn’t know what ax.spines.items() represented. So I printed it to the console print(ax.spines.items()).

What we see is a dictionary converted to a list of tuples with the location of the spine in words (right, top, left, bottom) as the first item in each tuple and the location of that spine as the second item in each tuple (as shown below). As a side note what .items() is doing is coverting the dictionary ax.spines to a list of tuples.

dict_items([(‘right’, <matplotlib.spines.Spine object at 0x7f9fc0142400>), (‘top’, <matplotlib.spines.Spine object at 0x7f9fc0142710>), (‘left’, <matplotlib.spines.Spine object at 0x7f9fc0142278>), (‘bottom’, <matplotlib.spines.Spine object at 0x7f9fc0142588>)])

One thing I found tricky when I first started learning python was tuple unpacking especially when it was used in for loops. I’m assuming you already know what this means based on your response above, but just to clarify…

In this for loop we are looping over each tuple in the list. Each tuple represents the location of the spine, specifically the top spine/border, the right spine/border, the left spin/border, and the bottom spin/border. For each of those tuples we are unpacking each value into a variable called key and spine so we have access to them in our for loop (unpacking here just means assign multiple values in a tuple to multiple variable names). We technically don’t even need the key (or first item in each tuple) we just need to tell maplotlib the location of the spine that we want to set invisible (we can think of .set_visible as drawing the border). Then we say don’t make the spine at this location visible by passing in False to .set_visible() and do that for all the spines in the chart.

I hope this clarifies things. Let me know if there was anything I didn’t explain clearly.
Bradon