Data Visualization with Matplotlib library, custom stylesheets and built-in styles in Google Colab

Data Visualization with Matplotlib library, custom stylesheets and built-in styles in Google Colab

Welcome back to this Google Colab
tutorial. In this video, we’re going to learn more about matplotlib library and how to customize matplotlib, its confiigurations and stylesheets. Matplotlib is a multi-platform data visualization library built on Numpy arrays. Here is what we’re going to do. We will import matplotlib.pyplot and numpy for the operation. Then we will use the directive to choose appropriate aesthetic styles for the plots. Here we will set the classic style, which ensures that the plots we create use the classic Matplotlib style. Any plt plot command will use a figure window to open but %matplotlib inline will only draw static images and sets the backend of matplotlib to the inline backend. Now we’re going to import cycler. and a single-entry cycler object can be used to easily cycle over a single style. We have to use cycler() function to link a key to series of values and key must be hash-able. These are the values. These color codes represents the respective colors. Why we’re doing this is because each time matplotlib loads, it defines a runtime configuration (rc) containing the default styles for every plot element you create. This group ‘axes’ defines the overall design of the axes of your plot. It has a list or tuple of group names, xtick and ytick that gives design of x-axis and y-axis each. This group ‘patch’ shows how the edges of the plot looks like. And this group ‘lines’ set the appropriate expansion within the plot area. You can use all this to customize the style of the plotting. Here we initialize variable ‘x’ and this function returns samples from a normal distribution with a mean of 0 and a variance of 1 where you’ll eventually get a upper or lower limits. And we plot the histogram using hist() function. If you see, the color of the bar graph is the first color defined in the cycler() function And the overall layout of the plot is what we customized using plt.rc() method earlier. Next we will print the line graph using this function. It returns random numbers sampled from a uniform distribution over the intervals [0,1] where the distribution has no upper or lower limits We can see 5 different lines with varying colors that was defined in the cycler() function. These colors are the first 5 colors that was defined within the function. Now we will initialize variable ‘y’ with the function np.linspace() that takes in 3 parameters that is starting number, stopping number and the number of sample. This will plot the wave with the red color and this will plot the wave with blue color. If you can see, this two colors are the first two colors defined in the cycler() function and the layout is also the same. Next we will plot a simple bar graph with simple data. The variable ‘axes_part’ will get the equi-distant bars in the x-axis. This set the heights in the y-axis and this will set bars in the x-axis. We can see figure similar to that of the histogram. The following figure will be the plotting of pie diagram. We will use the same simple data used in the plot of bar graph. We will also initialize the variable ‘explode’ This will allow us to plot the pie diagram. The following function accepts 6 parameters. Height will give the value of variable ‘height’, explode will enable us to have protrusion on that particular slice of the pie diagram, label will provide the different labels for different portions of different values of bars, autopct will automatically convert the values of height into percentage, shadow will give the shadow to the different slices of the pie diagram and startangle will have a start of calculating different angle from 90`. The function axe.axis() method will enable us to have equal aspect ratio that ensures pie diagram is drawn as a circle. We can see in the figure that different slices of pie diagram have different colors. That’s how we can customize the style of the different plottings using runtime configuration (rc) parameters. Now we’re going to define a function that creates a histogram and a line graph. This sets the random seed to 0 so the pseudo random numbers you get from random will start from the same point. This will create the plot with length 4 and width 11. And this will initialize a histogram and generates samples from the normal distribution. As for this, it will initialize a line graph with 4 lines with different colors. So when we call this function, we will get the two different plots, a histogram and a line graph. Now we know that we can customize the styles to our own accord. But even if you don’t create your own style, the stylesheets include very useful styles by default. Now what we’re going to do is, show some of the available styles and how to use it. Firstly, we have FiveThirtyEight style. It mimics the graphics found on the popular FiveThirtyEight website. Secondly, we have ggplot. Matplotlib’s ggplot style mimics the default styles from the ggplot package in the R language which is very popular visualization tool. This is how it looks like. Thirdly, we have Bayesian Methods for Hackers style also known as bmh. It features figures created with great consistent and visually appealing style And this is how it looks. Next we have dark background style. It is useful to create figures that is used in presentation. This style provides this outlook. Also we have grayscale style. If you want figures for a print publication that does not accept color figures, you might find this very useful. This is how it looks. There is also Seaborn style which is inspired by the Seaborn library. These styles are loaded automatically when Seaborn is imported. This is how the plot looks like. With all of these built in options for various plot styles, matplotlib becomes much more useful for both interactive visualization and understanding the data. For more information please browse through the documentation found on web. Thank you for listening and for the time.

Related Posts

Continuation Painting with Richard Prince

RICHARD PRINCE: You know, there's just lots of different things that I do. And sometimes I spend a day doing
Animator Vs. Cartoonist Draw More Spongebob Characters From Memory
Great Creative Cement Craftsmen – The Idea of ​​Making Coffee Tables from Bricks and Cement

Leave a Reply

Your email address will not be published. Required fields are marked *