适配Web应用且无渲染瑕疵的Matplotlib后端选型咨询
Matplotlib后端选择:无渲染瑕疵且适配Web应用的方案

我的图表出现了不必要的渲染瑕疵。为排查问题,我关闭了'agg'后端,瑕疵随即消失——此时Matplotlib使用的是默认后端,但我不清楚具体是哪一个。
我需要选用既不会产生渲染瑕疵,又适配Web应用的后端。Matplotlib后端文档提到:
还有一些后端可运行Web应用服务器以动态提供图表
之前我以为agg是Web场景的唯一选择,但现在发现它会导致渲染问题。我正在开发应用,但对Web开发完全不了解,想请教:哪些后端既能避免生成渲染瑕疵,又适合后续迁移到Web环境?
我可以自行测试后端来筛选无瑕疵的选项,但需要社区经验来确认适配Web的部分。
我已经根据建议完成了以下操作:
- 将matplotlib升级至3.9.2版本
- 将Pillow升级至10.4.0版本
- 将图片格式改为.png
在仍使用.jpeg格式时,两次升级未带来明显改善,但保存为.png格式后问题有所缓解,可能是版本更新与格式调整共同作用的结果。
以下是生成上述图表的代码:
import matplotlib as mpl import matplotlib.pyplot as plt from matplotlib.ticker import PercentFormatter, AutoMinorLocator import numpy as np from PIL import Image mpl.use('agg') plt.style.use('dark_background') my_figure = plt.figure(**{'dpi': 100.0, 'edgecolor': 'black', 'facecolor': 'black', 'figsize': (8.0, 6.0), 'linewidth': 1.0}) my_figure.suptitle(**{'fontsize': 'x-large', 'fontweight': 'semibold', 'horizontalalignment': 'center', 't': 'Binomial Distribution Pair Plot', 'verticalalignment': 'top'}) my_figure.supxlabel(**{'fontsize': 'large', 'fontweight': 'normal', 'horizontalalignment': 'center', 't': 'Failures', 'verticalalignment': 'bottom'}) my_figure.supylabel(**{'fontsize': 'large', 'fontweight': 'normal', 'horizontalalignment': 'left', 't': 'Probability', 'verticalalignment': 'center'}) axes_dictionary = my_figure.subplot_mosaic(mosaic=[['binomial_w_cumulative_overlay']] ,gridspec_kw={'wspace': 0.0, 'hspace': 0.0}) axes_dictionary['binomial_w_cumulative_overlay'].bar(**{'x': [0, 1, 2], 'height': [0.7778213593991469, 0.19641953520180488, 0.02380842850930969], 'label': 'individual probability', 'color': 'C0', 'zorder': 10}) axes_dictionary['binomial_w_cumulative_overlay'].bar(**{'x': [0, 1, 2], 'height': [0.7778213593991469, 0.9742408946009518, 0.9980493231102614], 'label': 'cumulative probability', 'color': 'C1', 'zorder': 5, 'alpha': 0.2}) axes_dictionary['binomial_w_cumulative_overlay'].set_xticks(ticks=[0, 1, 2], labels=[0, 1, 2]) axes_dictionary['binomial_w_cumulative_overlay'].set_ylim(ymax=1.0) axes_dictionary['binomial_w_cumulative_overlay'].yaxis.set_minor_locator(AutoMinorLocator(2)) axes_dictionary['binomial_w_cumulative_overlay'].yaxis.set_major_formatter(PercentFormatter(xmax=1.0)) my_figure.savefig('test_figure.jpg') Image.open('test_figure.jpg').show() plt.show()
内容的提问来源于stack exchange,提问作者Python_Learner
相关产品推荐
相关产品推荐

