Figure Caption jupyter notebook

I would like to add a figure caption to a jupyter notebook by add a code to the metacode but that doesn’t seem to work. I am following the instruction in this link:

http://blog.juliusschulz.de/blog/ultimate-ipython-notebook

It shows the figure caption as ???

{
  "caption": "somecaption",
  "hide_input": false,
  "label": "fig:somelabel",
  "widefigure": true,
  "trusted": true
}

At the site you linked to there it says that is what you should expect in the notebook and only after rendering with latex/pdf conversion will it show. That seems to be different than maybe what you want given the title of your post, perhaps?

There are other ways that will show in the active and static notebook. One is to combine markdown text and images. (The seventh cell here illustrates accomplishing something similar with code. You could, of course, do something similar in just a markdown code cell.) Another is to use Pillow or matplotlib to composite text and an image.

I would like to be able to refer to the figure no in my narrative and also show the figure no below the graph. Please can you help me by given an example? Thanks

What are you using to make your plots? That way maybe my example will be more in line with what you are using, but I cannot promise anything.

I am using matplotlib

The below is an example of what I am trying to do:

from matplotlib import pyplot as plt
import numpy as np
x=np.arange(-10,10,0.1)
plt.plot(x,x**3);

The easiest then is to use figtext(), see here.
Example incorporating your code:

from matplotlib import pyplot as plt
import numpy as np
x=np.arange(-10,10,0.1)
txt="Figure 3. Values collected by user4545 are plotted."
plt.figtext(0.5, 0.01, txt, wrap=True, horizontalalignment='center', fontsize=14)
plt.plot(x,x**3);

Thanks! The only issue doing this way is having to remember the figure no as it isn’t automated. Thanks for your help

But it could be or at least make it tracked elsewhere. You could use a generator to get the next available number. Or could make a dictionary or enum for it and track it that way. That way you can refer to it later via that variable, if you happen to be using python markdown for your narrative.

Was the way from the blog automated?

As an aside, JuptyerBook allows automatic numbering of equations and linking to them but I don’t think it supports numbering plots or images automatically (yet?).

Thanks for your help. I am having another issue where the text of the figure in conflicting with the text from the x axis. I have tried to play with the numbers to align it correct but that doesn’t seem to work. I have included an image of the issue. Thanks for your help.

You should just need to adjust the y value in the figtext placement.

Because it is always easier to have some code and examples that can be directly run when trying to debug, I made a gist of a demonstration notebook here. (In addition, I noted when trying to use the code I myself placed in code blocks at Discourse that the quotes messed up in the notebook it seems. So this allows you to use example code direct.)

If you need a place to easily run it, go here and press launch bendit and upload the notebook by drag and drop into the file navigation panel on the right, assuming you have already downloaded it to your local machine from the gist. There isn’t anything magical about that environment that launches in the Jupyter Lab session other than it has recent plotting packages and it was handy.

If your code isn’t behaving like that you want to post toy data with your code, I’d be happy to debug if it isn’t working well. One tip is that often you’ll find different behavior depending on when in the figure building process you invoke objects. Another thing I’d suggest testing things in a clean notebook in your environment. That way you can rule out some code earlier in your notebook causing an issue.

1 Like

Many Thansk for your help. That has worked fine.

1 Like