0
0
Matplotlibdata~30 mins

Named colors and hex codes in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Named colors and hex codes
📖 Scenario: You are creating a simple color chart to understand how named colors and hex codes work in plotting. This helps when you want to make your charts look nice and clear.
🎯 Goal: Build a small program that stores some named colors and their hex codes, then uses matplotlib to show these colors in a bar chart.
📋 What You'll Learn
Create a dictionary with color names as keys and their hex codes as values
Create a list of color names to use in the chart
Use matplotlib to plot a bar chart with bars colored by their hex codes
Print the dictionary of colors and hex codes
💡 Why This Matters
🌍 Real World
Knowing how to use named colors and hex codes helps you make charts and graphs that are easy to understand and visually appealing.
💼 Career
Data scientists and analysts often customize chart colors to match branding or to highlight important data points clearly.
Progress0 / 4 steps
1
Create a dictionary of colors and hex codes
Create a dictionary called colors with these exact entries: 'red': '#FF0000', 'green': '#00FF00', 'blue': '#0000FF', 'yellow': '#FFFF00', and 'purple': '#800080'.
Matplotlib
Need a hint?

Use curly braces {} to create the dictionary. Each entry is a key-value pair separated by a colon.

2
Create a list of color names to plot
Create a list called color_names containing these exact strings in this order: 'red', 'green', 'blue', 'yellow', 'purple'.
Matplotlib
Need a hint?

Use square brackets [] to create the list with the color names as strings.

3
Plot a bar chart with colors using hex codes
Import matplotlib.pyplot as plt. Then create a bar chart with bars for each color in color_names. Use the hex codes from colors to set the bar colors. Use range(len(color_names)) for x positions and [1]*len(color_names) for bar heights. Set the bar colors with the color parameter using a list comprehension that gets hex codes from colors.
Matplotlib
Need a hint?

Use plt.bar() with x positions as range(len(color_names)) and heights as a list of 1s. Use a list comprehension to get hex codes for colors.

4
Print the colors dictionary and show the plot
Print the colors dictionary. Then call plt.show() to display the bar chart.
Matplotlib
Need a hint?

Use print(colors) to show the dictionary. Use plt.show() to display the chart.