My chart has unwanted artifacts:
To troubleshoot I turned off the the 'agg'
backend and they disappeared. Matplotlib
used the default backend, I don’t know which one. I want to use a backend that doesn’t create artifacts yet is appropriate for web applications.
The documentation on backends states:
still others run web application servers to dynamically serve up
graphs
Based on the sparse documentation notes regarding that use case I believed agg
was the only choice. I am building an application and know nothing about web development (yet). My question is which backend(s) are appropriate to not generate artifacts, and will be appropriate when I move to the web?
I could test the backends to see which ones don’t generate artifacts but I need SO wisdom for the latter part of the question.
Per a comments:
- Upgraded to
matplotlib
3.9.2
- Upgraded to
Pillow
10.4.0
- Changed to .png format image
These two upgrades had no noticeable improvements to my problem while still using .jpeg
format but saving the figure as a .png
did. It may have been the combination of the updates and formatting.
Here’s the code to create the chart above:
<code>import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter, AutoMinorLocator
plt.style.use('dark_background')
my_figure = plt.figure(**{'dpi': 100.0,
my_figure.suptitle(**{'fontsize': 'x-large',
'fontweight': 'semibold',
'horizontalalignment': 'center',
't': 'Binomial Distribution Pair Plot',
'verticalalignment': 'top'})
my_figure.supxlabel(**{'fontsize': 'large',
'horizontalalignment': 'center',
'verticalalignment': 'bottom'})
my_figure.supylabel(**{'fontsize': 'large',
'horizontalalignment': 'left',
'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()
<code>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()
</code>
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()