Resolved: why we put an asterisk(*) in the line of code: plt.legend(*scatter.legend_elements())
in
The Complete Data Visualization Course with Python, R, Tableau, and Excel
/
Scatter Plot - Python - How to Create a Scatter Plot
Could you explain why we put an asterisk(*) before variable. What does the asterisk exactly do in the line of code in the picture below. Thank you
2 answers ( 1 marked as helpful)
Hi Thai,
thanks for reaching out! In this case, the asterisk means unpacking. Basically, it gives us a list of the elements of the scatter.legend_elements(). So, for example if we have a list:
mylist = ["A", "B", "C", "D"]
then print(*mylist)
will result in
A B C D
It's the same logic for the legend elements in the scatter plot.
Hope this helps!
Best,
365 Eli
Hi Elitsa,
Thank you for your quick response. I did some searching on the internet and I'm clearer now. Thank you.