Matplotlib Boxplot Color
Matplotlib is a popular Python library commonly used for creating data visualizations. Boxplots are a type of chart that summarize the distribution of a dataset, highlighting important statistical values such as the median, quartiles, and outliers. In this article, we will focus on customizing the color of boxplots in Matplotlib.
Basic Boxplot with Default Colors
To begin, let’s create a basic boxplot using Matplotlib with its default colors:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data)
plt.show()
Output:
Customizing Boxplot Colors
Matplotlib allows us to customize the colors of various elements in a boxplot, including the boxes, whiskers, medians, fliers, and caps. We can do this by specifying the color
parameter in the boxplot()
function.
Changing the Color of the Boxes
Let’s change the color of the boxes in a boxplot to red:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, boxprops=dict(color="red"))
plt.show()
Output:
Changing the Color of the Whiskers
Next, let’s change the color of the whiskers to green:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, whiskerprops=dict(color="green"))
plt.show()
Output:
Changing the Color of the Medians
We can also change the color of the medians in a boxplot. Let’s make the medians blue:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, medianprops=dict(color="blue"))
plt.show()
Output:
Changing the Color of the Fliers
To change the color of the fliers (outliers) in a boxplot, we can specify a different color. Let’s make the fliers orange:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, flierprops=dict(markerfacecolor="orange", marker='s'))
plt.show()
Output:
Changing the Color of the Caps
Lastly, let’s change the color of the caps on the whiskers to purple:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, capprops=dict(color="purple"))
plt.show()
Output:
Changing the Color of Multiple Elements
We can also customize the colors of multiple elements in a single boxplot by passing a dictionary of properties to the boxplot()
function.
Customizing Multiple Boxplot Colors
Let’s create a boxplot with customized colors for the boxes, whiskers, medians, fliers, and caps:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, boxprops=dict(color="red"), whiskerprops=dict(color="green"),
medianprops=dict(color="blue"), flierprops=dict(markerfacecolor="orange", marker='s'),
capprops=dict(color="purple"))
plt.show()
Output:
Using Hex Colors
In addition to specifying color names, we can also use hex color codes to define custom colors for boxplot elements.
Using Hex Colors for Box
Let’s create a boxplot with a custom hex color for the boxes:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, boxprops=dict(color="#FF5733"))
plt.show()
Output:
Using Hex Colors for Whiskers
We can also use hex color codes for the whiskers in a boxplot:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, whiskerprops=dict(color="#33FF57"))
plt.show()
Output:
Using Hex Colors for Medians
Similarly, we can specify hex colors for the medians in a boxplot:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, medianprops=dict(color="#3366FF"))
plt.show()
Output:
Using Hex Colors for Fliers
To use hex colors for the fliers in a boxplot, we can pass the hex color code as the markerfacecolor
:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, flierprops=dict(markerfacecolor="#FF33C7", marker='s'))
plt.show()
Output:
Using Hex Colors for Caps
Lastly, we can change the color of the caps using a hex color code:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, capprops=dict(color="#FFDD33"))
plt.show()
Output:
Customizing Outlier Markers
In addition to changing the color of the fliers, we can also customize the shape and size of the outlier markers in a boxplot.
Customizing Outlier Marker Shape and Size
Let’s customize the shape and size of the outlier markers by changing the marker
and markersize
properties:
import matplotlib.pyplot as plt
import numpy as np
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
plt.boxplot(data, flierprops=dict(marker='D', markersize=10))
plt.show()
Output:
Conclusion
In this article, we explored how to customize the colors of various elements in a boxplot using Matplotlib. By specifying the color
parameter with different properties, we can create visually appealing and informative boxplots that suit our data visualization needs. Experiment with different color combinations and styles to enhance the presentation of your boxplots in Matplotlib.