Plot Registered Django Users with Matplotlib

Sometimes you want to see the number of registered users in your Django website
over time. Fortunately Django stores the registration date of a user model
instance in the date_joined field, so we can plot it.

Prerequisites

You need an installation of numpy and matplotlib. You can install the
libraries using pip:

$ pip install numpy
$ pip install matplotlib

It is important that you install numpy before you install matplotlib,
otherwise compiling the latter won't work. You can also use your system package
manager to install them instead, if you prefer that.

Adding a Management Command

Now create a new Django management command. We could also do this as a
standalone script, but by creating a management command we have easier access to
the database.

fromoptparseimportmake_optionfromdjango.core.management.baseimportBaseCommandfromdjango.contrib.authimportget_user_modelimportnumpyasnpimportmatplotlib.pyplotaspltfrommatplotlib.datesimportdate2numclassCommand(BaseCommand):help='Plot user registrations. Use --save option to save the plot to a png file.'option_list=BaseCommand.option_list+(make_option('--save',action='store',dest='save',help='Save the graph as a png to the specified location'),)defhandle(self,*args,**options):# Get user join datesUser=get_user_model()datetimes=User.objects.values_list('date_joined',flat=True) \
.order_by('date_joined')dates=map(lambdad:d.date(),datetimes)# Get some auxilliary valuesmin_date=date2num(dates[0])max_date=date2num(dates[-1])days=max_date-min_date+1# Initialize X and Y axesx=np.arange(min_date,max_date+1)y=np.zeros(days)# Iterate over dates, increase registration arrayfordateindates:index=int(date2num(date)-min_date)y[index]+=1y_sum=np.cumsum(y)# Plotplt.plot_date(x,y_sum,xdate=True,ydate=False,ls='-',ms=0,color='#16171E')plt.fill_between(x,0,y_sum,facecolor='#D0F3FF')plt.title('Registered Users')plt.rc('font',size=8)ifoptions['save']:plt.savefig(options['save'])else:plt.show()

Usage

Now you can call your new management command:

$ python manage.py plot_user_registrations

This will open a new window containing the plot. You can also save it to a png
file instead:

$ python manage.py plot_user_registrations --save users.png

If you want, you can tweak the matplotlib configuration in order to change the
plot style / colors / etc. If you find some nice improvements, feel free to
leave a comment below!