0

I know there are a few similar questions related to this but I have absolutely no idea on what to do. I am absolutely a newbie when it comes to this so if possible a step-by-step guide would be extremely helpful. So I have been trying to write a simple code in Jupyter notebook:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('text', usetex=True)
plt.rcParams.update({'font.size': 22})

zeta = np.arange(10**-6,10**4,1000)
N0 = 50
ns = 1-(3*(1+16*zeta*N0/3))/(N0*(1+8*zeta*N0))

plt.plot(zeta,ns)
plt.show()

This is my code. And I am getting the following error:

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

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

~\anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
   2228                        else suppress())
   2229                 with ctx:
-> 2230                     self.figure.draw(renderer)
   2231 
   2232             if bbox_inches:

~\anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     72     @wraps(draw)
     73     def draw_wrapper(artist, renderer, *args, **kwargs):
---> 74         result = draw(artist, renderer, *args, **kwargs)
     75         if renderer._rasterizing:
     76             renderer.stop_rasterizing()

~\anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     49                 renderer.start_filter()
     50 
---> 51             return draw(artist, renderer, *args, **kwargs)
     52         finally:
     53             if artist.get_agg_filter() is not None:

~\anaconda3\lib\site-packages\matplotlib\figure.py in draw(self, renderer)
   2788 
   2789             self.patch.draw(renderer)
-> 2790             mimage._draw_list_compositing_images(
   2791                 renderer, self, artists, self.suppressComposite)
   2792 

~\anaconda3\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    130     if not_composite or not has_images:
    131         for a in artists:
--> 132             a.draw(renderer)
    133     else:
    134         # Composite any adjacent images together

~\anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     49                 renderer.start_filter()
     50 
---> 51             return draw(artist, renderer, *args, **kwargs)
     52         finally:
     53             if artist.get_agg_filter() is not None:

~\anaconda3\lib\site-packages\matplotlib\_api\deprecation.py in wrapper(*inner_args, **inner_kwargs)
    429                          else deprecation_addendum,
    430                 **kwargs)
--> 431         return func(*inner_args, **inner_kwargs)
    432 
    433     return wrapper

~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in draw(self, renderer, inframe)
   2919             renderer.stop_rasterizing()
   2920 
-> 2921         mimage._draw_list_compositing_images(renderer, self, artists)
   2922 
   2923         renderer.close_group('axes')

~\anaconda3\lib\site-packages\matplotlib\image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    130     if not_composite or not has_images:
    131         for a in artists:
--> 132             a.draw(renderer)
    133     else:
    134         # Composite any adjacent images together

~\anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     49                 renderer.start_filter()
     50 
---> 51             return draw(artist, renderer, *args, **kwargs)
     52         finally:
     53             if artist.get_agg_filter() is not None:

~\anaconda3\lib\site-packages\matplotlib\axis.py in draw(self, renderer, *args, **kwargs)
   1140 
   1141         ticks_to_draw = self._update_ticks()
-> 1142         ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
   1143                                                                 renderer)
   1144 

~\anaconda3\lib\site-packages\matplotlib\axis.py in _get_tick_bboxes(self, ticks, renderer)
   1066     def _get_tick_bboxes(self, ticks, renderer):
   1067         """Return lists of bboxes for ticks' label1's and label2's."""
-> 1068         return ([tick.label1.get_window_extent(renderer)
   1069                  for tick in ticks if tick.label1.get_visible()],
   1070                 [tick.label2.get_window_extent(renderer)

~\anaconda3\lib\site-packages\matplotlib\axis.py in <listcomp>(.0)
   1066     def _get_tick_bboxes(self, ticks, renderer):
   1067         """Return lists of bboxes for ticks' label1's and label2's."""
-> 1068         return ([tick.label1.get_window_extent(renderer)
   1069                  for tick in ticks if tick.label1.get_visible()],
   1070                 [tick.label2.get_window_extent(renderer)

~\anaconda3\lib\site-packages\matplotlib\text.py in get_window_extent(self, renderer, dpi)
    901 
    902         with cbook._setattr_cm(self.figure, dpi=dpi):
--> 903             bbox, info, descent = self._get_layout(self._renderer)
    904             x, y = self.get_unitless_position()
    905             x, y = self.get_transform().transform((x, y))

~\anaconda3\lib\site-packages\matplotlib\text.py in _get_layout(self, renderer)
    304 
    305         # Full vertical extent of font, including ascenders and descenders:
--> 306         _, lp_h, lp_d = renderer.get_text_width_height_descent(
    307             "lp", self._fontproperties,
    308             ismath="TeX" if self.get_usetex() else False)

~\anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in get_text_width_height_descent(self, s, prop, ismath)
    227             texmanager = self.get_texmanager()
    228             fontsize = prop.get_size_in_points()
--> 229             w, h, d = texmanager.get_text_width_height_descent(
    230                 s, fontsize, renderer=self)
    231             return w, h, d

~\anaconda3\lib\site-packages\matplotlib\texmanager.py in get_text_width_height_descent(self, tex, fontsize, renderer)
    399             dvifile = self.make_dvi(tex, fontsize)
    400             with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
--> 401                 page, = dvi
    402             # A total height (including the descent) needs to be returned.
    403             return page.width, page.height + page.descent, page.descent

~\anaconda3\lib\site-packages\matplotlib\dviread.py in __iter__(self)
    249             integers.
    250         """
--> 251         while self._read():
    252             yield self._output()
    253 

~\anaconda3\lib\site-packages\matplotlib\dviread.py in _read(self)
    329         while True:
    330             byte = self.file.read(1)[0]
--> 331             self._dtable[byte](self, byte)
    332             downs += self._dtable[byte].__name__ == "_down"
    333             if (self._baseline_v is None

~\anaconda3\lib\site-packages\matplotlib\dviread.py in wrapper(self, byte)
    172             if state is not None and self.state != state:
    173                 raise ValueError("state precondition failed")
--> 174             return method(self, *[f(self, byte-min) for f in get_args])
    175         if max is None:
    176             table[min] = wrapper

~\anaconda3\lib\site-packages\matplotlib\dviread.py in _fnt_def(self, k, c, s, d, a, l)
    474     @_dispatch(min=243, max=246, args=('olen1', 'u4', 'u4', 'u4', 'u1', 'u1'))
    475     def _fnt_def(self, k, c, s, d, a, l):
--> 476         self._fnt_def_real(k, c, s, d, a, l)
    477 
    478     def _fnt_def_real(self, k, c, s, d, a, l):

~\anaconda3\lib\site-packages\matplotlib\dviread.py in _fnt_def_real(self, k, c, s, d, a, l)
    481         tfm = _tfmfile(fontname)
    482         if tfm is None:
--> 483             raise FileNotFoundError("missing font metrics file: %s" % fontname)
    484         if c != 0 and tfm.checksum != 0 and c != tfm.checksum:
    485             raise ValueError('tfm checksum mismatch: %s' % n)

FileNotFoundError: missing font metrics file: cmss17

I am not sure if I should be placing the whole error here but this is what I am getting when I try plotting the graph. The only thing which I could think of is the fact that some file which is used to write things on the graph is missing but I don't know how to resolve this. Any help would be appreciated. Thank You.

Past21
  • 1
  • 1
  • It was good to post all the stuff you did. Rarely folks do. However, you don't mention what you are trying to do with your code? I can tell you, unless you are trying to do something super complex that isn't yet built-in to matplotlib now, there's usually no need anymore for use of `rc` and `rcParams` any longer. Your code works if you remove the 4th and 5th lines that include that related code. You can go [here](https://mybinder.org/v2/gh/fomightez/communication_voila/HEAD) open a new notebook and paste in your code without those lines and see it work. – Wayne Mar 15 '22 at 19:50
  • Thank you so much ! It does work now. Someone suggested me to use rc and rcParams, so I was doing that. But is there any reason as to why using those doesn't work ? I had used those lines previously as well but till then it worked absolutely fine. However, the above error started to show up just a few days prior. – Past21 Mar 16 '22 at 02:21
  • At this point, if it's working with the more modern code, I'd encourage you not to worry too much. In theory rc should still work, but I think the `'text', usetex=True` part may be an issue. You could try [this](https://stackoverflow.com/a/3900167/8508004) that doesn't involve that? If you insist on unearthing the issue... DId something get updated when the above error start. Maybe something is mismatched now? Or matplotlib itself got updated and it didn't get tested with that aspect because it isn't the typical practice any more? – Wayne Mar 16 '22 at 02:49
  • Ok. I see. I will check out if this happened. Thank you so much for the help. – Past21 Mar 16 '22 at 04:07

2 Answers2

1

I just came across the same error, in my case I needed to use latex. I found out that if other fonts are correctly installed, changing the default font can resolve the issue:

plt.rcParams.update({
"text.usetex": True,
"font.family": "sans-serif",
"font.sans-serif": ["Helvetica"]})

You could insert this script at line 6 of your code and see if it helps.

Sina Mansour L.
  • 418
  • 4
  • 8
0

I didn't follow from what you posted what you were looking to be font size 22, and so this does it for the labels of the axis ticks.
Simpler version of what you posted:

import numpy as np
import matplotlib.pyplot as plt

zeta = np.arange(10**-6,10**4,1000)
N0 = 50
ns = 1-(3*(1+16*zeta*N0/3))/(N0*(1+8*zeta*N0))
plt.plot(zeta,ns)
plt.tick_params(labelsize=22)
plt.show()

You can go to here in my comment, open a notebook after the session spins up, and test that code there. You should see it should work. If it doesn't work the same way where you are trying you have something out of date.

The tick formatting basics are illustrated right at the top of Matplotlib's tick formatters documentation in the first block of code. You shouldn't need rc or rcParams any longer in most cases. The syntax is much more obvious now. Any answers using that are either outdated as they aren't using the provided routes or they are covering unusual / edge cases.

Wayne
  • 6,607
  • 8
  • 36
  • 93