Contenido del Curso
Ultimate Visualization with Python
Ultimate Visualization with Python
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()
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()
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()
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.
Tarea
- Set the color of the lowest bars to
'darkslateblue'
. - Set the color of the middle bars to
'steelblue'
(the argument should follow thelabel
parameter). - Set the transparency of the middle bars to
0.7
(the rightmost argument). - Set the color of the top bars to
'goldenrod'
.
¡Gracias por tus comentarios!
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()
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()
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()
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.
Tarea
- Set the color of the lowest bars to
'darkslateblue'
. - Set the color of the middle bars to
'steelblue'
(the argument should follow thelabel
parameter). - Set the transparency of the middle bars to
0.7
(the rightmost argument). - Set the color of the top bars to
'goldenrod'
.
¡Gracias por tus comentarios!
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()
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()
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()
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.
Tarea
- Set the color of the lowest bars to
'darkslateblue'
. - Set the color of the middle bars to
'steelblue'
(the argument should follow thelabel
parameter). - Set the transparency of the middle bars to
0.7
(the rightmost argument). - Set the color of the top bars to
'goldenrod'
.
¡Gracias por tus comentarios!
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()
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()
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()
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.
Tarea
- Set the color of the lowest bars to
'darkslateblue'
. - Set the color of the middle bars to
'steelblue'
(the argument should follow thelabel
parameter). - Set the transparency of the middle bars to
0.7
(the rightmost argument). - Set the color of the top bars to
'goldenrod'
.