Thursday, August 1, 2019

Using Matplotlib Built-in Styles to customize plots

Matplotlib has a number of predefined styles available, with good starting settings for background colors, gridlines, line widths, fonts, font sizes, and more that will make your visualizations appealing without requiring much customization. To see the styles available on your system, run the following lines in a terminal session:

C:\Users\python>python
Python 3.6.5rc1 (v3.6.5rc1:f03c5148cf, Mar 14 2018, 03:12:11) [MSC v.1913 64 bit
 (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> plt.style.available


['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'gray
scale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn
-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook',
 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-t
icks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', 'tabl
eau-colorblind10', '_classic_test']

>>>

To use any of these styles, add one line of code before starting to generate the plot. For example to use seaborn we can use plt.style.use('seaborn') as shown in the code below:

import matplotlib.pyplot as plt

input_values = [1, 2, 3, 4, 5]
cubic_values = [1,8,27,64,125]
plt.style.use('seaborn')
fig,ax = plt.subplots()
ax.plot(input_values,cubic_values, linewidth=3)

# Setting chart title and label axes
ax.set_title("Cube Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Cube of Value", fontsize=14)

# Set size of tick labels.
ax.tick_params(axis='both', labelsize=14)
plt.show()

The output of the program is shown below:


If we use the classic style we can use plt.style.use('seaborn') in our code and our output should change to the figure shown below:


A wide variety of styles is available; use these styles in your programs as per the desired look and feel.

Scatter() method

Sometimes, it’s useful to plot and style individual points based on certain characteristics. For example, you might plot small values in one color and larger values in a different color. You could also plot a large data set with one set of styling options and then emphasize individual points by replotting them with different options.

To plot a single point, use the scatter() method. Pass the single (x, y) values of the point of interest to scatter() to plot those values:



Now let's style our output by adding a title, label the axes, and make sure all the text is large enough to read. See the code below:

import matplotlib.pyplot as plt

plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.scatter(2, 4, s=200)
# Setting chart title and label axes
ax.set_title("Cube Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Cube of Value", fontsize=14)

# Set size of tick labels.
ax.tick_params(axis='both', labelsize=14)
plt.show()

In the above program we call scatter() and use the s argument to set the size of the dots used to draw the graph. When we run the above program we see a single dot in the middle of the chart as shown below:


To plot a series of points, we can pass scatter() separate lists of x- and y values as shown in the following program:

import matplotlib.pyplot as plt

x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]

plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.scatter(x_values, y_values, s=100)
# Setting chart title and label axes
ax.set_title("Cube Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Cube of Value", fontsize=14)

# Set size of tick labels.
ax.tick_params(axis='both', labelsize=14)
plt.show()

The x_values list contains the numbers to be cubed, and y_values contains the cubic value of each number. When these lists are passed to scatter(), Matplotlib reads one value from each list as it plots each point. The points to be plotted are (1, 1), (2, 4), (3, 9), (4, 16), and (5, 25); Figure below shows
the result:



Now let's modify our program so that we use a loop in Python to do the calculations for us rather than passing our points in a list . See the program below:

import matplotlib.pyplot as plt

x_values = range(1,101)
y_values = [x**3 for x in x_values]
plt.style.use('seaborn')
fig, ax = plt.subplots()
ax.scatter(x_values, y_values, s=10)
# Setting chart title and label axes
ax.set_title("Cube Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Cube of Value", fontsize=14)

# Set size of tick labels.
ax.tick_params(axis='both', labelsize=14)

# Set the range for each axis.
ax.axis([0, 110, 0, 110000])
plt.show()

We start with a range of x-values containing the numbers 1 through 1000. Then a list comprehension generates the y-values by looping through the x-values (for x in x_values), cubing each number (x**3) and storing the results in y_values. We then pass the input and output lists to scatter(). As this is a large data set, we use a smaller point size. Finally we use the axis() method to specify the range of each axis. The axis() method requires four values: the minimum and maximum values for the x-axis and the y-axis. Here, we run the x-axis from 0 to 110 and the y-axis from 0 to 110000. When we run the program, the output will be as shown below:



In the next post we'll see how to change the color of the points. Till we meet next keep practicing and learning Python as Python is easy to learn!.
Share:

0 comments:

Post a Comment