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 Graphics
object.
I had to do it like this:
myplot = list_plot(
zip(range(20), range(20)),
color='red',
legend_label='legend item 1')
myplot2 = list_plot(
zip(range(20), [i*2 for i in range(20)]),
color='blue',
legend_label='legend item 2')
combined = myplot + myplot2
combined.axes_labels(['testing x axis', 'testing y axis'])
combined.legend(True)
combined.show(title='Testing title', frame=True, legend_loc="lower right")
I'm not entirely certain why there's no title
method and why the title has to be specified inside show
when the axes don't have to be, but this does appear to work.