How to show a many-to-many field with “list_display” in Django Admin?

You may not be able to do it directly. From the documentation of list_display ManyToManyField fields aren’t supported, because that would entail executing a separate SQL statement for each row in the table. If you want to do this nonetheless, give your model a custom method, and add that method’s name to list_display. (See below …

Read more

How to place inline labels in a line plot

Update: User cphyc has kindly created a Github repository for the code in this answer (see here), and bundled the code into a package which may be installed using pip install matplotlib-label-lines. Pretty Picture: In matplotlib it’s pretty easy to label contour plots (either automatically or by manually placing labels with mouse clicks). There does …

Read more

How to bind an unbound method without calling it?

All functions are also descriptors, so you can bind them by calling their __get__ method: bound_handler = handler.__get__(self, MyWidget) Here’s R. Hettinger’s excellent guide to descriptors. As a self-contained example pulled from Keith’s comment: def bind(instance, func, as_name=None): “”” Bind the function *func* to *instance*, with either provided name *as_name* or the existing name of …

Read more

How to change the color of the axis, ticks and labels

As a quick example (using a slightly cleaner method than the potentially duplicate question): import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.plot(range(10)) ax.set_xlabel(‘X-axis’) ax.set_ylabel(‘Y-axis’) ax.spines[‘bottom’].set_color(‘red’) ax.spines[‘top’].set_color(‘red’) ax.xaxis.label.set_color(‘red’) ax.tick_params(axis=”x”, colors=”red”) plt.show() Alternatively [t.set_color(‘red’) for t in ax.xaxis.get_ticklines()] [t.set_color(‘red’) for t in ax.xaxis.get_ticklabels()]