Home > database >  Where is a mistake in this code, which raises ValueError?
Where is a mistake in this code, which raises ValueError?

Time:01-28

I have a code:

fig, ax = plt.subplots(figsize=(5,8))
ax.barh(top20_deathtoll['Country_Other'], top20_deathtoll['Total_Deaths'], color='red', linewidth=0.45)             
ax.set_xlabel('Total Deaths', fontsize=10)
ax.set_ylabel('Country', fontsize=10)
ax.set_title('Top 20 Countries by Total Deaths', fontsize=12)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False) 
ax.tick_params(left=False, bottom=False, labelleft=True, labelbottom=True) 
ax.xaxis.set_ticks([0, 150000, 300000]) 
ax.xaxis.set_tick_params(labelbottom=False, labeltop=True, color='grey')
plt.title("The Death Toll Worldwide Is 1.5M ", x=-80000, y=23.5, fontsize=17, fontweight="bold")
plt.tight_layout()
plt.show()

When I run following code it raises a value error:

ValueError: Image size of 22320474x10268 pixels is too large. It must be less than 2^16 in each direction.

I wonder what image is mentioned in traceback and what can I do to fix it.

EDIT Here is the first part of traceback, I can't post all of it baceause then there's too much code in my question. I hope this will be enough, forgive me if I didn't understand something obvious cause I'm a quite newbie:

ValueError                                Traceback (most recent call last)
File ~\programs\anaconda3\lib\site-packages\IPython\core\formatters.py:339, in BaseFormatter.__call__(self, obj)
    337     pass
    338 else:
--> 339     return printer(obj)
    340 # Finally look for special method names
    341 method = get_real_method(obj, self.print_method)

File ~\programs\anaconda3\lib\site-packages\IPython\core\pylabtools.py:151, in print_figure(fig, fmt, bbox_inches, base64, **kwargs)
    148     from matplotlib.backend_bases import FigureCanvasBase
    149     FigureCanvasBase(fig)
--> 151 fig.canvas.print_figure(bytes_io, **kw)
    152 data = bytes_io.getvalue()
    153 if fmt == 'svg':

File ~\programs\anaconda3\lib\site-packages\matplotlib\backend_bases.py:2319, in FigureCanvasBase.print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
   2315 try:
   2316     # _get_renderer may change the figure dpi (as vector formats
   2317     # force the figure dpi to 72), so we need to set it again here.
   2318     with cbook._setattr_cm(self.figure, dpi=dpi):
-> 2319         result = print_method(
   2320             filename,
   2321             facecolor=facecolor,
   2322             edgecolor=edgecolor,
   2323             orientation=orientation,
   2324             bbox_inches_restore=_bbox_inches_restore,
   2325             **kwargs)
   2326 finally:
   2327     if bbox_inches and restore_bbox:

CodePudding user response:

Like @fdireito said, please try to change the coordinates in plt.title() line. These ones, which you passed are way too far from your plot's limit so when program try to put them in the place you chose it gets too large image.

  • Related