4

我目前正在使用 sagemath 托管的在线工作簿制作一些图表。

这是我正在尝试生成图表的一些代码示例:

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()

这是非常基本的——它本质上是两个并列的散点图。

有没有办法轻松添加轴标签、图例和可选的标题?

我设法破解了一个可以让我添加轴标签的解决方案,但它看起来非常丑陋和愚蠢。

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')

有没有更简单的方法来添加轴标签、图例和标题?

4

2 回答 2

8

我最终找到了sagemath对象的文档。Graphics

我不得不这样做:

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")

我不完全确定为什么没有title方法以及为什么在不需要轴时必须在内部指定标题show,但这似乎确实有效。

于 2012-09-29T17:59:18.740 回答
-1
  • 轴标签:myplot.xlabel("text for x axis"),myplot.ylabel("text for y axis")
  • 标题:myplot.title("My super plot")
  • 图例:label="Fancy plot"为调用添加一个参数plot并创建图例legend()

请参阅此处此处以获取更多说明。

于 2012-09-29T06:07:07.627 回答