Questions about the churn rate chart

Hi! Could you help explain the code below?
1,what do the first 2 lines for? looks like we import numpy and matplotlib here, but what is the arrange and ellipse for in the right side?
2,There is marker='.'in the line 3, what does it mean?
3, What are line 4 to line 7?
4,in the last line, it seems that we want to circle certain number on the chart. what are 5,0.065 for?

Thank you in advance!

arange = __import__("numpy").arange
Ellipse = __import__("matplotlib").patches.Ellipse
ax = churn.plot(x="yearmonth", y="churn_rate", figsize=(12,6), rot=45, marker=".")
start, end = ax.get_xlim()
ax.get_xticks()
ax.set_xticks(arange(2, end, 3))
ax.set_xticklabels(yearmonths[2::3])
circle = Ellipse((35, churn.loc[churn.yearmonth == "201312", "churn_rate"].iloc[0]),
                 5, 0.065, color='sandybrown', fill=False
                   )```

It’s not importing numpy or matplotlib, it’s from numpy import arange and from matplotlib.patches import Ellipse. That’s why you can use arange() on line 6 and Ellipse() on line 8. If you import numpy only, you have to do numpy.arange() , longer syntax

Go to matplotlib docs, find a sample plot using marker parameter (there are endless examples), look at what other possible values marker accepts, and use those other values. This is also how any other library beyond matplotlib can be learned. Something less commonly known is you can turn a line-plot into a scatter-plot just by changing the marker, no need to .plot(kind='scatter') when pandas plotting.

Ablation study, delete 1 by 1, look at output and make a guess? After that, leave it there and tweak inputs and make more guesses, finally open docs and confirm your guess.
It’s strange start, end are not used anywhere, also ax.get_xticks() return values did not get assigned or used. probably line 4-5 can be deleted and 6-7 alone gives same effect.

The normal use of get is to get objects before feeding the returned objects as inputs into setting methods like plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False).

I forgot for which exact methods, but compared to ax.xxx api, plt.xxx api makes it simpler by automatically getting the latest object for you so you can set directly without explicitly getting before set. However, using plt api gives you less control because it’s state-based (you only have access to the latest version of all objects in plot) vs ax.xxx api which is object-oriented. https://realpython.com/python-matplotlib-guide/

Look at the documentation, then count the arguments. 5, 0.065 are the 2nd,3rd argument, so is width,height. Note that almost every matplotlib function has **kwargs, it takes some digging to find out what they are. Normally these kwargs are common across functions so you can fill them from memory, and you also won’t need those you cannot recall unless you want very customized look.

Thank you for your explanation.

No chapter talked about the usage of Ellipse before, where can I find more information? We do fro numpy import arrange before, here the format is arange = __import__("numpy").arange any reason we have to use _ ?

Noted the marker usage.

How to understand `` plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False) ?

I have never seen Ellipse too in last 2 years.
I just googled “matplotlib ellipse” and read the docs to answer.

There are many underscore patterns in python, some are naming conventions, some actually affect program behaviour: https://dbader.org/blog/meaning-of-underscores-in-python#:~:text=Single%20underscores%20are%20a%20Python,hint%20to%20the%20programmer%20only.
Double Underscore you see here __import__ allows you to do inline importing and assign to a variable. It’s the same as writing the import the usual way on it’s own line.

Don’t need know exactly what this is doing, because i copied this from a long chunk of sample code on matplotlib and i didn’t study what it’s doing. Just look at the syntax and learn what’s possible/what goes where. Basically setp is used to set properties in a plot. Inside this call is a list comprehension to get all the axes in the figure f and extract their xticklabels property.
Setp docs here: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.setp.html
You can open any of the examples below the page to see how any function is used. Same learning pattern as sklearn docs which has examples below too.

The alternating cycle is

  1. Read code from anywhere, see unknown method, open docs to read about method
  2. Open docs examples using that method, go back to 1

If can’t find docs, google anything with programcreek, they contain code samples scraped from github.
Can’t guarantee correct logic but at least highly likely the syntax works. You can even learn advanced patterns not shown/or too convoluted to read in docs.

Thank you for sharing all the methods!! it helps!