Mic*_*x2a 4 python graphing sage
我目前正在使用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()
Run Code Online (Sandbox Code Playgroud)
这是非常基本的 - 它基本上是两个并排在一起的散点图.
有没有办法轻松添加轴标签,图例和可选的标题?
我设法破解了一个让我添加轴标签的解决方案,但它看起来非常丑陋和愚蠢.
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')
Run Code Online (Sandbox Code Playgroud)
是否有更简单的方法来添加轴标签,图例和标题?
我最终找到了 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")
Run Code Online (Sandbox Code Playgroud)
我不完全确定为什么没有title方法以及为什么必须show在轴内部指定标题,但这确实有效.
| 归档时间: |
|
| 查看次数: |
7861 次 |
| 最近记录: |