Writing numerical values on the plot with Matplotlib

Hi all,
How to place text annotations at any x and y values we want. I am doing something like this.
(source: https://stackoverflow.com/questions/6282058/writing-numerical-values-on-the-plot-with-matplotlib

release = netflix_df[[‘type’, ‘release_year’]].copy()

y = release.groupby([‘release_year’]).size()
x = release.release_year

mov_release = release[release[‘type’]==‘Movie’]
tv_release = release[release[‘type’]==‘TV Show’]

plt.figure(figsize=(30, 12))
fig = plt.figure()
ax = fig.add_subplot(111)

release.groupby([‘release_year’]).size().plot( kind=‘line’, label = “Total”, lw=3)

for i,j in zip(x,y):
ax.annotate(str(j),xy=((i,j+0.5)), xytext=(-10,10), textcoords=‘offset points’)

mov_release.groupby([‘release_year’]).size().plot( kind=‘line’, marker=‘s’,label = “Movies”, lw=3)
for i,j in zip(x,y):
ax.annotate(str(j),xy=(i,j), xytext=(-10,10), textcoords=‘offset points’)

tv_release.groupby([‘release_year’]).size().plot( kind=‘line’, marker=‘s’, label = “TV Shows”, lw=3)
for i,j in zip(x,y):
ax.annotate(str(j),xy=(i,j), xytext=(-10,10), textcoords=‘offset points’)

plt.title(“Total content added across all years (up to 2018)”)
plt.ylabel(“Releases”)
plt.xlabel(“Year”)
plt.xlim([2002, 2018])
plt.legend()
plt.show();

The image I am getting is:

Can you use this instead of annotation?

import matplotlib.pyplot as plt

x=[1,2,3]
y=[9,8,7]

plt.plot(x,y)
for a,b in zip(x, y): 
    plt.text(a, b, str(b))
plt.show()

If you want to use annotations, visit this site, it’s more clear here.

Also, I would suggest you use Plotly. Pointing out in Plotly is way easier and it makes the graph interactive.

Thanks for the suggestion.
I tried

for a,b in zip(x, y):
plt.text(a, b, str(b))
But it didn’t solve the issue:

My code looks like this:

release = netflix_df[[‘type’, ‘release_year’]].copy()
y_axis = release.groupby([‘release_year’]).size()
x_axis = release.release_year
mov_release = release[release[‘type’]==‘Movie’]
tv_release = release[release[‘type’]==‘TV Show’]
fig = plt.figure()
ax = fig.add_subplot(111)
release.groupby([‘release_year’]).size().plot( kind=‘line’, label = “Total”, lw=3)
for i,j in zip(x_axis,y_axis):
plt.text(i,j, str(j))
mov_release.groupby([‘release_year’]).size().plot( kind=‘line’, marker=‘s’,label = “Movies”, lw=3)
for i,j in zip(x_axis,y_axis):
plt.text(i,j, str(j))
tv_release.groupby([‘release_year’]).size().plot( kind=‘line’, marker=‘s’, label = “TV Shows”, lw=3)
for i,j in zip(x_axis,y_axis):
plt.text(i,j, str(j))
plt.title(“Total content added across all years (up to 2018)”)
plt.ylabel(“Releases”)
plt.xlabel(“Year”)
plt.xlim([2002, 2018])
plt.legend()
plt.show();

I will have a look at Plotly and see if that solves the issue…

Cheers!

1 Like

Can you please share the notebook link?

Sure, here it is:

https://jovian.ai/kiranpatel4192/netflix-movies-and-shows

1 Like