Matplotlib,动态改变绘图宽度 [英] Matplotlib, dynamically changing width of plot
问题描述
所以我有一个完全黑色的 matplotlib 图,所有轴都被禁用,图中唯一存在的是白色文本,包含一个数学表达式,由用户输入,作为参数传递给渲染函数'expr'.
So i have a matplotlib plot that is completely black with all axes disabled, the only thing present on the plot is white text, containing a mathematical expression, entered by the user, which is passed into the render function as the parameter 'expr'.
def render(expr, path, width, x, y):
fig = plt.figure(figsize=(width, 16 if 'begin' in expr else 12))
plt.text(x, y, expr, fontsize=320, fontweight='bold', color='white')
fig.set_facecolor('#000000')
plt.axis('off')
plt.savefig(path, facecolor=fig.get_facecolor())
plt.clf()
'width'是图的宽度,目前我有一个非智能系统,该系统在纯文本表达式的长度和图的宽度之间建立了联系,但它不适用于矩阵图有多条线路,系统明显不能很好地工作.
'width' is the width of the plot, currently i have an unintelligent system in place that strikes a relationship between the length of the plaintext expression and the width of the plot, but it does not work exactly especially with a matrix plot which has multiple lines, the system in place obviously doesn't work very well at all.
我希望根据白色文本在整个绘图上的延伸程度来计算绘图所需的宽度,因此一旦文本横向完成,我希望绘图在那里结束,有没有办法做到这一点?/检测白色文本何时会在情节上结束?
I want the required width of the plot to be calculated based on how much the white text stretches across the plot, so as soon as the text finishes widthwise, i want the plot to end there, is there a way of doing this?/ detecting when the white text would come to an end on the plot?
这样是为了让表达式后面没有很多黑色空间,拖慢渲染到 .png 的时间,浪费空间.由于绘图与表达式的宽度相同
This is so that there is not a lot of black space after the expression, slowing down rendering time to .png and wasting space. As the plot is the same width as the expression
推荐答案
在github上有完整的可用解决方案:
There is a full working solution available on github:
将 LaTeX 方程绘制为 matplotlib 图形 by ahwillia
https://gist.github.com/ahwillia/c7e54f875913ebc3de3852e>cc69f>
https://gist.github.com/ahwillia/c7e54f875913ebc3de3852e9f51ccc69
<小时>github 上还有另一个模块,它(也)可在命令行上运行:
There is also another module available on github which (also) works on the command line:
texscrap - johnjosephhorton
https://github.com/johnjosephhorton/texscrap
这篇关于Matplotlib,动态改变绘图宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!