centering matplotlib inline plots in ipython notebooks?

2,089 views
Skip to first unread message

dataholiks...@gmail.com

unread,
Jun 10, 2015, 3:41:43 PM6/10/15
to jup...@googlegroups.com
Hi,

Apologies if this is not the correct place to ask this question. Is there an *easy* way to center inline plots in an ipython notebook? I have looked around online but could not find any pointers except perhaps writing a custom css file.

Dataholik.

Brian Granger

unread,
Jun 10, 2015, 7:23:33 PM6/10/15
to jup...@googlegroups.com
CSS is going to be the way of getting this done. I don't see any other
way for now.
> --
> You received this message because you are subscribed to the Google Groups
> "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jupyter+u...@googlegroups.com.
> To post to this group, send email to jup...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jupyter/a7805442-8d73-4ed3-aa35-8761703f8be2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian E. Granger
Cal Poly State University, San Luis Obispo
@ellisonbg on Twitter and GitHub
bgra...@calpoly.edu and elli...@gmail.com

Matthias Bussonnier

unread,
Jun 10, 2015, 7:27:54 PM6/10/15
to jup...@googlegroups.com

> On Jun 10, 2015, at 16:23, Brian Granger <elli...@gmail.com> wrote:
>
> CSS is going to be the way of getting this done. I don't see any other
> way for now.

The problem is the container are flexible to allow things to overflow. So getting the css to center things with unknown width might be hard.

M
> To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/CAH4pYpT%2B_SwQKVBqha-d3fJoROA4pn7t0ptg%2By_n2vuLAEZc_g%40mail.gmail.com.

Maximilian Albert

unread,
Jun 10, 2015, 8:00:08 PM6/10/15
to jup...@googlegroups.com
A quick-and-dirty way of doing it would be to wrap the matplotlib figure in a class which provides a custom _html_repr_() method so that the appearance can be tweaked. Here is a quick example:


(This requires Python 3; in principle it also works with Python 2 but some of the imports and definitions need to be adapted. Also, it may not be necessary to include the raw image data in the <img> tag. This is just copied & pasted from some code that I had lying around where I needed to do it like this.)

I appreciate it's rather kludgy and I'm not recommending to do it this way, but something along these lines might provide a quick fix if you can't get it to work any other way.

Cheers,
Max



dataholiks...@gmail.com

unread,
Jun 11, 2015, 10:32:39 AM6/11/15
to jup...@googlegroups.com
Thanks all for your suggestions. I am familiar with html/css/json but have never dug into the notebook format/raw files. I suppose it's time to roll up my sleeves and get dirty :).
Reply all
Reply to author
Forward
0 new messages