Data:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.DataFrame(np.random.normal(size=(15,4)))
#Rename columns
data.set_axis(['Column A', 'Column B', 'Column C', 'Column D'], axis=1, inplace=True)
data
Column A Column B Column C Column D
0 0.786186 -0.416792 0.174680 2.487244
1 -0.252369 -0.342730 0.205828 -1.321883
2 -2.000831 -1.710470 1.230441 1.151613
3 1.589489 -0.735494 -1.427740 -0.291532
4 0.162657 0.091248 -1.166623 -1.702915
5 -2.046027 0.538372 1.799922 -1.283141
6 -0.046736 -0.100009 -0.775107 1.778775
7 -0.205502 -1.033712 0.335681 0.178957
8 -0.598907 1.863979 -0.828703 -0.977883
9 -0.532970 -0.964670 -1.618440 0.169850
10 2.123033 0.472480 2.307614 -0.397944
11 1.149670 -0.906352 0.409004 -1.322099
12 0.618216 -1.181656 0.342085 -0.853023
13 -1.108748 -0.546607 -3.468131 -0.382351
14 -0.404277 -1.612273 0.787983 1.033892
#Create figure
fig, axs = plt.subplots(2, figsize = (15,15))
#Colors I want to use
colors = ['#002072', '#00BDF2']
I want to create a box plot of my data in the axs[0] subplot (the top subplot in the figure I have created) and fill in the bar colors as shown below. Notice how the colors alternate. I have provided the hex color codes of the colors I want. I also want to change the color of the median/mean line so that it's easier to see in the dark blue bars. I really appreciate any help. I've been struggling with this for hours. You would think it would be as simple as passing an argument, but I have not had success with the example code I've found online. I get more confused the more examples I come across.
Thank you!
CodePudding user response:
Your question is a bit too broad, let's start with just the one question of coloring. After this, look through the documentation on your own, do a little bit of Googling... and if you still can't figure out the excess parts of your question, feel free to make a new question~
CodePudding user response:
I like to suggest you this code but, you should improve the code based on your data.
# Import libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Creating dataset
np.random.seed(10)
data_1 = np.random.normal(100, 10, 200)
data_2 = np.random.normal(90, 20, 200)
data_3 = np.random.normal(80, 30, 200)
data_4 = np.random.normal(70, 40, 200)
data = [data_1, data_2, data_3, data_4]
fig = plt.figure(figsize =(10, 7))
ax = fig.add_subplot(111)
# Creating axes instance
bp = ax.boxplot(data, patch_artist = True,
notch ='True', vert = 0)
colors = ['#0000FF', '#00FF00',
'#FFFF00', '#FF00FF']
for patch, color in zip(bp['boxes'], colors):
patch.set_facecolor(color)
# changing color and linewidth of
# whiskers
for whisker in bp['whiskers']:
whisker.set(color ='#8B008B',
linewidth = 1.5,
linestyle =":")
# changing color and linewidth of
# caps
for cap in bp['caps']:
cap.set(color ='#8B008B',
linewidth = 2)
# changing color and linewidth of
# medians
for median in bp['medians']:
median.set(color ='red',
linewidth = 3)
# changing style of fliers
for flier in bp['fliers']:
flier.set(marker ='D',
color ='#e7298a',
alpha = 0.5)
# x-axis labels
ax.set_yticklabels(['Column A', 'Column B',
'Column C', 'Column D'])
# Adding title
plt.title("Customized box plot")
# Removing top axes and right axes
# ticks
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
# show plot
plt.show()
The Output Customize Box plot