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

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:

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()
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:

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()
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:

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()
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.

Tehtävä

Swipe to start coding

  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'.

Ratkaisu

import matplotlib.pyplot as plt
import numpy as np
countries = ['USA', 'China', 'Japan']
primary_sector = np.array([1.4, 4.8, 0.4])
secondary_sector = np.array([11.3, 6.2, 0.8])
tertiary_sector = np.array([14.2, 8.4, 3.2])
# Set the correct color
plt.bar(countries, primary_sector, label='primary sector', color='darkslateblue')
# Set the correct color and transparency value
plt.bar(countries, secondary_sector, bottom=primary_sector, label='secondary sector', color='steelblue', alpha=0.7)
# Set the correct color
plt.bar(countries, tertiary_sector, bottom=primary_sector + secondary_sector, label='tertiary sector', color='goldenrod')
plt.legend()
plt.show()

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 4
import matplotlib.pyplot as plt
import numpy as np
countries = ['USA', 'China', 'Japan']
primary_sector = np.array([1.4, 4.8, 0.4])
secondary_sector = np.array([11.3, 6.2, 0.8])
tertiary_sector = np.array([14.2, 8.4, 3.2])
# Set the correct color
plt.bar(countries, primary_sector, label='primary sector', ___='___')
# Set the correct color and transparency value
plt.bar(countries, secondary_sector, bottom=primary_sector, label='secondary sector', ___='___', ___=___)
# Set the correct color
plt.bar(countries, tertiary_sector, bottom=primary_sector + secondary_sector, label='tertiary sector', ___='___')
plt.legend()
plt.show()

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt