Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Colors | Section
Data Visualization & EDA
Abschnitt 1. Kapitel 12
single

single

bookColors

Swipe um das Menü anzuzeigen

Colors

When bar charts were discussed, the colors of individual bars were customized. To change the color for all plots uniformly, use the color keyword argument.

123456789101112131415161718
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

In this case, the red color was set for the first line plot, and the second one was set to blue. Unlike scatter or bar plots, which consist of multiple elements, a line plot represents a single element, so only one color can be assigned to it. For comparison, consider the following bar chart example from an earlier section:

123456789
import matplotlib.pyplot as plt programming_languages = ['Python', 'Java', 'C#', 'C++'] shares = [40, 30, 17, 13] # Setting a separate 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.

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

Aufgabe

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

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 12
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt