I have some 2d data points that come with labels that I would like to render in pygal. For example say that for each point I need to visualise a feature when I over on it with the mouse.
First of all, to plot the points without any labels, I did the following:
import pygal #
data = [(0, 0), (.1, .2), (.3, .1), (.5, 1), (.8, .6), (1, 1.08), (1.3, 1.1), (2, 3.23), (2.43, 2)]
xy_chart = pygal.XY(stroke=False)
xy_chart.title = 'Correlation'
xy_chart.add('A', data)
xy_chart.render()
And it plots all the data as I wish, without labels (meaning that if I over with the mouse on a point, it gives me only the class 'A' and the coordinates of the point).
Now, In order to add labels to my data, how should I modify the code? Initially, I tried to do this:
xy_chart.add('A', [{'value': data, 'label': r'$\tau = 2.3'}])
But it gives me error unsupported operand type(s) for -: 'tuple' and 'tuple
If I consider only one element of the list per time, like:
first_elem = data[0]
xy_chart.add('A', [{'value': first_elem, 'label': r'$\tau = 2.3'}])
it works without errors, but it does not render in latex the labels, so I see in the image $\tau = 2.3$
instead of the rendered latex code.
So two problems: the first one is how can I put labels on points in general and the second one is how do I render those labels in Latex
Regarding your second question: I'm not sure pygal supports LaTeX (I wasn't able to find any information about this — correct me if I'm wrong).
Regarding your first question: populate your data list with dictionaries as follows:
data = [{'value': (0, 0), 'label': "τ = 2.3"},
(.1, .2), (.3, .1), (.5, 1), (.8, .6), (1, 1.08), (1.3, 1.1),
(2, 3.23), (2.43, 2)]
...
xy_chart.add('A', data)
...
(I only showed it for the first term of your list).
By the way, you're able to include some unicode characters so, unless you want labels with involved mathematical formulas, you should be able to get some symbols (like τ
here).
You can use list comprehensions to programmatically determine your labels. E.g.,
data = [(0, 0), (.1, .2), (.3, .1), (.5, 1), (.8, .6), (1, 1.08), (1.3, 1.1), (2, 3.23), (2.43, 2)]
labelled_data = [{'value': k, 'label': "τ = 2.3"} for k in data]
...
xy_chart.add('A', labelled_data)
...