0

I'm trying to center the title in a grouped bar chart.

By reading the docs (), I thought I could manage to do this by specifying the orient and anchor parameters in the configure_title method.

This however does not seem to be working for me. I tried several values for the anchor parameter but it looks like it is not applied and sticks to its default value.

Is there something I'm missing?

I am working with the version 2.4.1 of altair.

import altair as alt
import pandas as pd


# dataset
df = pd.DataFrame([[1, 'A', 'G1'],
                   [2, 'A', 'G2'],
                   [5, 'B', 'G1'],
                   [10, 'B', 'G2'],
                   [4, 'C', 'G1'],
                   [9, 'C', 'G2'],
                   [1, 'D', 'G1'],
                   [1, 'D', 'G2'],
                   [4, 'E', 'G1'],
                   [9, 'E', 'G2'],
                   [8, 'F', 'G1'],
                   [1, 'F', 'G2'],
                   [2, 'G', 'G1'],
                   [3, 'G', 'G2'],
                   [2, 'H', 'G1'],
                   [1, 'H', 'G2'],
                   [3, 'I', 'G1'],
                   [8, 'I', 'G2'],
                   [7, 'J', 'G1'],
                   [5, 'J', 'G2'],
                  ], columns=['Count', 'Category', 'Group'])


chart = alt.Chart(df, title=f"Count of categories in each group",
         width=50, height=600).mark_bar().encode(   column=alt.Column('Category:O'),
                                                    color=alt.Color('Group', type='nominal', scale=alt.Scale(range=['#1f77b4', '#2ca02c'])),
                                                    x=alt.X(field="Group", type="nominal",
                                                            axis=None,
                                                            title="Category",
                                                           ),
                                                    y=alt.Y(field="Count", type="quantitative",
                                                            axis=alt.Axis(title="Number of records",
                                                                          titlePadding=10.0),
                                                           ),
)
chart = chart.configure_title(fontSize=20, offset=5, orient='top', anchor='middle')

# display the chart
chart

Chart with the title top left instead of top middle

The title remains at the top left instead of being at the top middle.

Jose Manuel
  • 25
  • 1
  • 7

1 Answers1

1

This is an issue in Vega-Lite 2 that has been fixed in Vega-Lite 3. Once you update to Altair version 3, it should work as expected without any change to your code: enter image description here

jakevdp
  • 77,104
  • 11
  • 125
  • 160
  • Dear Jake, thanks for your swift reply! Is there any trick to update altair to version 3? I am using conda and could not find a way to achieve this. I tried with my env (python 3.6.6) as well as with a fresh env with python 3.7.3 using the conda-forge channel. I could not upgrade vega either (still 2.0.1). – Jose Manuel Apr 15 '19 at 16:15
  • Currently you can install the pre-release with `pip install altair==3.0.0rc1`. A final 3.0 release will be happening very soon. – jakevdp Apr 15 '19 at 16:32
  • Note, however, that you'll need to update your frontend extension (jupyterlab, notebook, etc.) to be compatible with vega-lite 3. – jakevdp Apr 15 '19 at 16:34
  • Thanks again Jake, but I am noticing a strange behavior with my env (python 3.7.3 on Kubuntu 18.04)... I updated jupyterlab to latest release (0.35.4) and altair to version 3.0.0rc1 on python 3.7.3. However, when I import altair, I still get the old version being displayed with alt.__version__ (2.4.1) and the title still does not align to the middle of the plot. The correct version of altair (3.0.0rc1) is displayed when using conda list or directly in terminal, so I guess there is an extra step I am missing to make jupyterlab compatible with vega-lite 3? – Jose Manuel Apr 15 '19 at 20:25
  • It sounds like you installed altair 3 in a different location than the kernel you are using in jupterlab (make sure you are running pip install with the pip connected to the python kernel you are using in Jupyterlab; see https://stackoverflow.com/questions/39007571/running-jupyter-with-multiple-python-and-ipython-paths/39022003#39022003). Also, once you figure that out, you will need the jupyterlab 1.0 pre-release in order to view Altair 3 plots (this is one reason we haven't released Altair 3 yet). – jakevdp Apr 15 '19 at 21:01
  • Dear Jake, you are absolutely correct, there is problem with the kernel I am using. When printing sys.executable from within the jupyter-lab, I get the path to another environment than the one I started the jupter-lab with. I observe the same behavior with jupyter-notebook, but not with python or ipython directly in terminal, where the correct path is printed. I am quite unsure about why this is happening... Unfortunately, I'll be able to investigate more only in a few days, so I'll post the results then. Thank you again for your help! – Jose Manuel Apr 16 '19 at 08:05
  • Dear Jake, sorry for the delay. So I updated my env to altair 3.0.0rc1. In jupyter-lab, plots are not displayed anymore. I get instead a text linking to an url for help. I tried to apply the indications there at https://altair-viz.github.io/user_guide/troubleshooting.html#notebook-vega-lite-3-object, but it did not solve the issue. The plot is displayed in jupyter-notebook, but the title is not printed. I just used the same code as above. Any idea why I don't get the same result as yours? Thanks! – Jose Manuel Apr 25 '19 at 21:04
  • Altair 3 has not yet been released, so the user guide has not been updated. If you are using jupyterlab, Altair 3 requires jupyterlab 1.0 or newer (which also has not yet been released). – jakevdp Apr 25 '19 at 22:10
  • The only reason this is not working is obviously because I am having issues with the installation of altair itself instead of the code for the plot. So I'm validating your solution and will try again when Altair 3 is released. Thank you again for your kind help! – Jose Manuel Apr 29 '19 at 18:23
  • Altair 3.0 was released a few days ago. – jakevdp Apr 29 '19 at 19:31
  • I'm having the same issue as the OP (with the title not centering) and it would appear I'm using Altair 3. At least, when I run `conda search altair`, it says the version number is 3.2.0 – Ragnar Lothbrok Dec 07 '19 at 18:12
  • It works correctly for me in Altair 3.2 and 3.3. You should double-check what version of Altair you are using via ``print(alt.__version__)``. The ``conda search`` command tells you what versions are available to install, not what versions you have installed. – jakevdp Dec 07 '19 at 18:45