Matplotlib, dynamically changing width of plot

自闭症网瘾萝莉.ら 提交于 2020-01-25 17:26:22

问题


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

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


回答1:


There is a full working solution available on github:

Plot a LaTeX equation as a matplotlib figure by ahwillia

https://gist.github.com/ahwillia/c7e54f875913ebc3de3852e9f51ccc69


There is also another module available on github which (also) works on the command line:

texscrap - Python utility for making PDFs out of scraps of LaTeX by johnjosephhorton

https://github.com/johnjosephhorton/texscrap



来源:https://stackoverflow.com/questions/41200414/matplotlib-dynamically-changing-width-of-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!