I’m currently making some graphs using the online workbook hosted by sagemath.
This is an example of some code I’m making to try and generate a graph:
myplot = list_plot(zip(range(20), range(20)), color='red')
myplot2 = list_plot(zip(range(20), [i*2 for i in range(20)]), color='blue')
combined = myplot + myplot2
combined.show()
It’s very basic — it’s essentially two scatter plots juxtaposed on top of each other.
Is there a way to easily add axis labels, a legend, and optionally a title?
I managed to hack out a solution that lets me add axis labels, but it looks really ugly and stupid.
from matplotlib.backends.backend_agg import FigureCanvasAgg
def make_graph(plot, labels, figsize=6):
mplot = plot.matplotlib(axes_labels=labels, figsize=figsize)
mplot.set_canvas(FigureCanvasAgg(mplot))
subplot = mplot.get_axes()[0]
subplot.xaxis.set_label_coords(x=0.3,y=-0.12)
return mplot
a = make_graph(combined, ['x axis label', 'y axis label'])
a.savefig('test.png')
Is there an easier way to add axis labels, legend, and a title?
I eventually found the documentation for sagemath’s
Graphicsobject.I had to do it like this:
I’m not entirely certain why there’s no
titlemethod and why the title has to be specified insideshowwhen the axes don’t have to be, but this does appear to work.