Customizing the appearance of plots

In the preceding output, we can see that all of the lines makes telling the difference between them difficult. To customize the appearance of the violin plots, we must return the value of plot n, and put it into something so that we can look at it. Hence, when we print this vplot, we get a dictionary with some keys:

# Show Median & Means
vplot = plt.violinplot(dists, showmeans=True, showmedians=True);
print(vplot)

We will get the following output:

The keys are as follows:

dict_keys(['bodies', 'cmeans', 'cmaxes', 'cmins', 'cbars', 'cmedians'])

Each of the keys corresponds to the different components of the plot. The 'bodies' key is a list of objects used for the distributions, while the 'cmedians', 'cmaxes', 'cBar', 'cmeans', and 'cmins' keys are all of the line elements.

We will change the color of the medians so that we can see the difference between medians and the means. Hence, by typing vplot ['cmedians'], we can set the color to blue. The following code shows the difference between these medians and the means:

# Getting the components
vplot = plt.violinplot(dists, showmedians=True, showmeans=True)
vplot['cmedians'].set_color('b')

Following is the output of the preceding code:

To make the bodies a different color, we need to access each individual distribution. Index the plots like an array so that the first distribution has a black background and the second has a green background. Leave the third as it is:

The violin plot requires a different syntax for each of the components. set_color takes a list or a tuple of colors. While passing 1, it will simply pass the same value to each of the colors. In terms of the rest of the violin plots, we can use the same kinds of arguments that we passed to the box plot to flip them, label them, and so on.

In the next section, we're going to take a look at how to visualize ordinal and tabular data, where one of your axes isn't really a number.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset