我目前正在尝试绘制具有六个级别的分组条形图,如下面的代码所示:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
font = {'family': 'serif',
'color': 'darkgreen',
'weight': 'normal',
'size': 12,
}
# Create dataframe
raw_data = {'first_name': ['City 1', 'City 2', 'City 3', 'City 4', 'City 5', 'City 6', 'Total '],
'A': [11.5, 17.9, 27.3, 10.1, 10.9, 11.1, 13.7],
'B': [15.1, 34.5, 28.8, 21.9, 25.8, 26.7, 24.2],
'C': [42.1, 31.0, 28.8, 37.5, 22.6, 38.9, 33.8],
'D':[25.0, 5.4, 8.3, 17.0, 24.0, 14.4, 17.2],
'E':[4.4, 4.8, 4.5, 4.5, 2.7, 5.6, 4.3],
'F':[2.0, 6.5, 2.3, 9.0, 14.0, 3.3, 6.9]}
df = pd.DataFrame(raw_data, columns = ['first_name','A', 'B', 'C', 'D', 'E','F'])
df
# Make plot
# Setting the positions and width for the bars
#pos = list(range(len(df['A'])))
pos = np.arange(len(df['A']))
width = 0.15
# Plotting the bars
fig, ax = plt.subplots(figsize=(10,5))
# Create a bar with pre_score data,
# in position pos,
plt.bar(pos,
#using df['pre_score'] data,
df['A'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='#008B45',
# with label the first value in first_name
label=df['first_name'][0])
# Create a bar with mid_score data,
# in position pos + some width buffer,
plt.bar([p + width for p in pos],
#using df['mid_score'] data,
df['B'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='#00FF7F',
# with label the second value in first_name
label=df['first_name'][1])
# Create a bar with post_score data,
# in position pos + some width buffer,
plt.bar([p + width*2 for p in pos],
#using df['post_score'] data,
df['C'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='#00F5FF',
# with label the third value in first_name
label=df['first_name'][2])
# Create a bar with post_score data,
# in position pos + some width buffer,
plt.bar([p + width*3 for p in pos],
#using df['post_score'] data,
df['D'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='red',
# with label the third value in first_name
label=df['first_name'][3])
# Create a bar with post_score data,
# in position pos + some width buffer,
plt.bar([p + width*4 for p in pos],
#using df['post_score'] data,
df['E'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='blue',
# with label the third value in first_name
label=df['first_name'][4])
# Create a bar with post_score data,
# in position pos + some width buffer,
plt.bar([p + width*5 for p in pos],
#using df['post_score'] data,
df['F'],
# of width
width,
# with alpha 0.5
alpha=0.5,
# with color
color='#5B5B5B',
# with label the third value in first_name
label=df['first_name'][5])
#params = {"text.color" : "blue"}
#plt.rcParams.update(params)
# Set the y axis label
ax.set_ylabel('Percentage')
# Set the chart's title
plt.title('Title here', fontdict=font)
# Set the position of the x ticks
ax.set_xticks([p + 1.5 * width for p in pos])
# Set the labels for the x ticks
ax.set_xticklabels(df['first_name'])
# Setting the x-axis and y-axis limits
plt.xlim(min(pos)-width, max(pos)+width*4)
plt.ylim(0,100)
# Adding the legend and showing the plot
plt.legend(['Very satisfied', 'Satisfied',
'Little satisfied', 'Not satisfied',
'There is no... ', 'No response'], loc='best', shadow = 'True', fontsize = 'small')
#plt.grid()
plt.show()
代码运行良好,并在下面生成此图:
因此,我需要两点帮助: 1.从图中可以看出,在右侧未显示“总计”中所有类别的六个级别。如何解决这个问题? 2.我想包括要在每个条形图上显示的值标签。可以请教如何做吗?
希望很快能收到您的来信。
此致
Rungo。