Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Colors and Transparency | Plots Customization
Ultimate Visualization with Python
course content

Conteúdo do Curso

Ultimate Visualization with Python

Ultimate Visualization with Python

1. Matplotlib Introduction
2. Creating Commonly Used Plots
3. Plots Customization
4. More Statistical Plots
5. Plotting with Seaborn

book
Colors and Transparency

Colors

When we discussed bar charts we customized the colors of the bars, setting a unique color for each separate bar. As a matter of fact, it is possible to change the color/colors for all the plots using the color keyword argument. Let’s have a look at an example:

1234567891011121314
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 data_log = np.exp(data_linear) # Setting the color of the first line plot plt.plot(data_linear, label='linear function', color='red') # Setting the color of the second line plot plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) plt.legend() plt.show()
copy

Here we set the red color for the first line plot, while the second line plot was set to the blue color. Unlike scatter plots or bar plots (they have multiple elements), we can only set one color for the line plot, since it only has one element. Speaking of the bar charts, here is an example from the previous section:

1234567
import matplotlib.pyplot as plt programming_languages = ['Python', 'Java', 'C#', 'C++'] shares = [40, 30, 17, 13] # Setting separate a color for each bar plt.bar(programming_languages, shares, color=['b', 'green', 'red', 'yellow']) plt.title('Percentage of users of programming languages') plt.show()
copy

Transparency

Another appearance parameter is alpha (transparency of the plot). Its default value is 1 (opaque), which is its maximum possible value. Basically, its possible values range from 0 to 1, where 0 makes the plot fully transparent.

Let’s modify our line plots with this parameter:

123456789101112
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 # Changing the transparency of the first line plot plt.plot(data_linear, label='linear function', color='red', alpha=0.5) plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) plt.legend() plt.show()
copy

Using alpha=0.5 we made the plot for the linear function more transparent in order to focus more attention on the quadratic function plot. Modifying transparency is mostly used exactly for this purpose.

Tarefa
test

Swipe to show code editor

  1. Set the color of the lowest bars to 'darkslateblue'.
  2. Set the color of the middle bars to 'steelblue' (the argument should follow the label parameter).
  3. Set the transparency of the middle bars to 0.7 (the rightmost argument).
  4. Set the color of the top bars to 'goldenrod'.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 4
toggle bottom row

book
Colors and Transparency

Colors

When we discussed bar charts we customized the colors of the bars, setting a unique color for each separate bar. As a matter of fact, it is possible to change the color/colors for all the plots using the color keyword argument. Let’s have a look at an example:

1234567891011121314
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 data_log = np.exp(data_linear) # Setting the color of the first line plot plt.plot(data_linear, label='linear function', color='red') # Setting the color of the second line plot plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) plt.legend() plt.show()
copy

Here we set the red color for the first line plot, while the second line plot was set to the blue color. Unlike scatter plots or bar plots (they have multiple elements), we can only set one color for the line plot, since it only has one element. Speaking of the bar charts, here is an example from the previous section:

1234567
import matplotlib.pyplot as plt programming_languages = ['Python', 'Java', 'C#', 'C++'] shares = [40, 30, 17, 13] # Setting separate a color for each bar plt.bar(programming_languages, shares, color=['b', 'green', 'red', 'yellow']) plt.title('Percentage of users of programming languages') plt.show()
copy

Transparency

Another appearance parameter is alpha (transparency of the plot). Its default value is 1 (opaque), which is its maximum possible value. Basically, its possible values range from 0 to 1, where 0 makes the plot fully transparent.

Let’s modify our line plots with this parameter:

123456789101112
import matplotlib.pyplot as plt import numpy as np data_linear = np.arange(0, 11) data_squared = data_linear ** 2 # Changing the transparency of the first line plot plt.plot(data_linear, label='linear function', color='red', alpha=0.5) plt.plot(data_squared, '-o', label='quadratic function', color='blue') plt.xticks(data_linear) plt.xlabel('x', loc='right') plt.ylabel('y', loc='top', rotation=0) plt.legend() plt.show()
copy

Using alpha=0.5 we made the plot for the linear function more transparent in order to focus more attention on the quadratic function plot. Modifying transparency is mostly used exactly for this purpose.

Tarefa
test

Swipe to show code editor

  1. Set the color of the lowest bars to 'darkslateblue'.
  2. Set the color of the middle bars to 'steelblue' (the argument should follow the label parameter).
  3. Set the transparency of the middle bars to 0.7 (the rightmost argument).
  4. Set the color of the top bars to 'goldenrod'.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 4
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt