sympy.plotting.plot 奇怪的 xlabel 位置 [英] sympy.plotting.plot strange xlabel position

查看:38
本文介绍了sympy.plotting.plot 奇怪的 xlabel 位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 sympy 的 plot 语句中将 'xlabel' 设置为字符串值时,标签放置在图形的最右侧.如果程序中没有设置标签,我点击复选标记并将标签设置在那里,它位于 X 轴的中间.

When setting 'xlabel' to a string value in a plot statement in sympy, the label is placed at the far right of the graph. If the label is not set in the program, and i click on the checkmark and set the label there, it's positioned in the middle of the X axis.

这是一个例子:

from sympy import sin
from sympy import symbols
from sympy.plotting import plot

x = symbols('x')
y = sin(x)

p1=plot(y,(x,0,7),xlabel="x data")
p2=plot(y,(x,0,7))

图 1 的最右侧有 X 轴标签.如果您打开图 2,然后单击顶部的复选标记以交互方式设置标签,则会将其正确放置在轴的中间.

figure 1 has the X-axis label to the far right. if you open figure 2, and click the checkmark at the top to set the label interactively, it places it correctly in the middle of the axis.

有什么方法可以指示程序将标签居中吗?

any way to instruct the program to centre the label?

推荐答案

Sympy 用于绘图的 matplotlib 实现很方便,但与完整的 matplotlib 功能相比相当有限.如果要更改轴上的标签位置,则需要从绘图访问后端 ax 对象.操作方法如下:

Sympy's implementation of matplotlib for plotting is convenient, but rather limited compared to the full matplotlib functionality. If you want to change the label location on the axis you need to access the backend ax object from your plot. Here's how to do that:

fig = p1._backend.fig
ax = p1._backend.ax

ax.xaxis.set_label_coords(0.5,0)

fig.savefig('test.png')

这会将您的图形保存为 test.png(见图).如果您使用的是 jupyter 之类的 gui,您还可以通过调用 fig 来显示绘图,但在命令行界面中这是行不通的.我不知道仅使用 sympy 界面(因此无需导入 matplotlib.pyplot)显示绘图的任何便捷方法(如 p1.show() 会).但是,如果您知道如何使用 matplotlib,则可以更详细地操作图形和轴对象.

This will save your graph as test.png (see picture). If you are using a gui like jupyter you can also show the plot by calling fig, but in command line interfaces this will not work. I do not know any convenient way to show the plot (like p1.show() would) using only the sympy interface (so without importing matplotlib.pyplot). If you know how to use matplotlib however you can manipulate both the figure and axis objects in far more detail.

这篇关于sympy.plotting.plot 奇怪的 xlabel 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆