How to add headers, axis label and legend in sagemath?

I am currently doing some graphics using an online book hosted by sagemath.

This is an example of the code I'm doing to try to create 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() 

This is very simple - there are essentially two scatterplots mapped to each other.

Is there a way to easily add axis labels, legend, and possibly a title?

I managed to crack a solution that allows me to add axis labels, but it looks very 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 title?

+4
source share
2 answers

I ended up finding documentation for the sagemath 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 quite sure why there is no title method and why the title should be indicated inside the show , when the axes should not be, but this seems to work.

+7
source
  • Axis labels: myplot.xlabel("text for x axis") , myplot.ylabel("text for y axis")
  • Title: myplot.title("My super plot")
  • Legend: add the label="Fancy plot" argument to the plot calls and create a legend using legend()

See here and there for further explanations.

-1
source

Source: https://habr.com/ru/post/1436924/


All Articles