This course covers the important aspects of building visualizations in Pygal, using styles and configurations to govern chart behavior, rendering and exporting to SVG and other specialized formats, and serving Pygal visualizations in web content.
Vector image formats such as SVG possess many important advantages over scalar formats such as PNG and JPEG. Using SVG, you can build high-quality, compact visualizations that render on low-resolution devices and that can be scaled, zoomed, and moved without distortion.
In this course, Pygal: Python Data Playbook, you will gain the ability to construct an array of visualizations and render them to SVG format using Pygal.
First, you will learn the advantages of working with Pygal for building SVGs and understand the niche that Pygal occupies relative to other visualization packages such as Matplotlib, Seaborn, Bokeh, and Plotly.
Next, you will discover how to build an array of visualizations in Pygal, from in-memory as well as file data. You will then construct a visualization including simple charts such as Line, Tree, and Bar graphs, as well as specialized types like TreeMaps and Sparklines. You will understand the different types of Styles and Configurations that can be used to govern chart appearance. You will work with built-in, parametric, and custom styles, as well as Chart, Serie, and Value configurations.
Finally, you will explore how to render Pygal visualizations to a range of image and non-image formats, including XML element trees and base64 encoded formats for online transfer. You will round out the course by building a web application using the Flask microframework in order to render and serve Pygal charts.
When you are finished with this course, you will have the skills and knowledge of building and rendering visualizations in Pygal needed to effectively harness the many advantages of the Scalable Vector Graphics format.
Course Overview Hi, and welcome to this course, Pygal: Python Data Playbook. My name is Kishan Iyer, and I will be your instructor for this course. A little about myself first. I have a masters degree in computer science from Columbia University and have previously worked in companies such as Deutsche Bank on WebMD in New York. I presently work for Loonycorn, a studio for high‑quality video content. In this course, you will gain the ability to construct an array of visualizations and render them to SVG format using Pygal. First, you will learn the advantages of working with Pygal for building SVGs and understand the niche that Pygal occupies relative to other visualization packages such as Matplotlib, seaborn, Bokeh, and Plotly. Next, you will discover how to build an array of visualizations in Pygal from in memory, as well as file data. You will then construct visualizations, including simple charts such as line, tree, and paragraph, as well as specialized types such as treemaps and sparklines. You will understand the different types of styles and configurations that can be used to govern chart appearance, and you will also work with built‑in parametric and custom styles, as well as chart, theory, and value configurations. Finally, you will explore how to render Pygal visualizations to a range of image on non‑image formats, which include XML element trees and Base64 encoded formats for online transfer. You will round out the course by building a web application using the Flask microframework in order to render and serve Pygal charts. When you're finished with this course, you will have the skills and knowledge of building and rendering visualizations in Pygal, which are needed to effectively harnessed the many advantages of the Scalable Vector Graphics format.
Rendering Out Charts In this demo, we will generate sparklines using Pygal. A sparkline can be thought of as a smaller version of a graph without any axes or labels. If you think of a graph as representing a summary of the underlying data, well, a sparkline is an even shorter summary. We will now go ahead and demonstrate this in a new Jupyter notebook and we will start off by importing the Pygal library and some of the classes and methods needed to render plots within a Jupyter notebook. The data which we will model are the close prices of the Apple stock over a 7 trading day period, and this information will be summarized using a line plot and we will label the line Apple. So this is the line plot which was generated, and we can gather from just looking at this plot that it starts off at a little under $166, it remains somewhat steady for 3 days, and then drops sharply and steadily until it dips to $147. So what I just narrated is a summary of what we see. An even shorter summary would be the Apple stock price starts off high and then drops. This is exactly what a sparkline will display and we can now render one by calling the render sparkline method of a Pygal chart. This sparkline will be in the SVG format and we then make use of the SVG class and the display method in order to render it within the Jupyter notebook. And what we end up with is this rather brief summary of the Apple stock price, starts high and then drops and ends low. There are no axes to speak of over here, so we don't know how many days of information this represents of what the actual stock prices were. This is why sparklines are often used to simply convey trends or to give a very succinct overview of the data. If you find that this particular sparkline is a little too reductive and you'd like to represent a little more information and perhaps style the sparkline as well, we will now do exactly that where we generate this line chart to use the DarkSolarizedStyle and to also include x_labels. So once we generate this line chart, it represents the same information, but has a nice dark color to it. When we convert this to a sparkline, on this occasion, we will explicitly define the height and width of the sparkline plot and we set the show_dots parameter to True so that the sparkline also includes the dots for each of the points which make up the line. And the result is a stylized version of the sparkline and we have one more point of data over here. We can see from the dots that this represents 7 days of information. In fact, we can add even more data to the sparkline where we set it to show x and y labels along the axes, and the new sparkline includes that information as well. Similar to sparklines, there is also something called sparktext, which is a bar version of the line plot. We can see what this looks like by calling the render_sparktext method of the line chart and what we get is a bar version of the sparkline and the number of bars indicates the number of data points along the x axis. So this sparktext represents the absolute value of the Apple stock close prices. If you want the sparktext to represent prices relative to a certain number, we can set the relative_to parameter to that value. So on this occasion, the Apple stock prices are rendered relative to the price of 130.