Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a perpendicular x axis line in pygal?

Tags:

pygal

I'd like to put in an axspan similar to the following provided in matplotlib so that I have a vertical line in my graph to point out a location. This is for a line chart using pygal

plt.axvspan(fin_loc, fin_loc+1, color='red', alpha=0.5)

like image 879
tsar2512 Avatar asked Jul 19 '18 19:07

tsar2512


1 Answers

It seems that pygal doesn't support custom additional drawings on its charts.

However vertical lines can be drawn with XY type charts. For example:

chart = pygal.XY()
chart.add('Here is axvspan', [(1, -5), (1, 5)])

But pygal also doesn't support multiple chart types on one drawing.

So you can draw vertical lines AND line chart with workaround: replace your line chart with XY chart type, draw your vertical line, and then draw your line chart. As XY chart needs two values for each point, you feed it with dummy sequential X values. For example, instead of:

chart = pygal.Line()
chart.add('Chart1', [16, 25, 31])

you write:

chart = pygal.XY()
chart.add('Here is axvspan', [(1, -5), (1, 5)])
chart.add('Chart1', [(1, 16), (2, 25), (3, 31)])

And that's it!

like image 99
SergeyLebedev Avatar answered Oct 08 '22 07:10

SergeyLebedev