Python matplotlib.pyplot
Python matplotlib.pyplot
我在这个条形图上的标签错了一个,我想不出如何更正它。我曾尝试将另一个系列添加到索引中,但这会产生错误。这与条的宽度有关吗?我迷路了。
import matplotlib.pyplot as plt
import numpy as np
labels = ['cluster1','cluster2','cluster3','cluster4','cluster5','cluster6','cluster7']
Mean = df_mean['net_sale']
Count = df_p_count
Sum = df_p_sum
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, Count, width, label='% of Sales')
rects2 = ax.bar(x + width/2, Sum, width, label='% of Revenue')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Percent')
ax.set_title('Discount Promotion Analysis')
ax.set_xticks(x, labels)
#ax.set_xticklabels(labels)
ax.set_xticklabels(['cluster1','cluster2','cluster3','cluster4','cluster5','cluster6','cluster7'])
ax.legend()
fig.tight_layout()
plt.show()
Graph Output
您的代码无法生成正确可视化的原因是 ax.set_xticks(x, labels)
是错误的,因为 set_xticks
需要一个长度与 labels
相同的数字数组。
labels = ['cluster1','cluster2','cluster3','cluster4','cluster5','cluster6','cluster7']
# Mean = df_mean['net_sale']
# Count = df_p_count
# Sum = df_p_sum
Count = [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1]
Sum = [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, Count, width, label='% of Sales')
rects2 = ax.bar(x + width/2, Sum, width, label='% of Revenue')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Percent')
ax.set_title('Discount Promotion Analysis')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
fig.tight_layout()
plt.show()
我在这个条形图上的标签错了一个,我想不出如何更正它。我曾尝试将另一个系列添加到索引中,但这会产生错误。这与条的宽度有关吗?我迷路了。
import matplotlib.pyplot as plt
import numpy as np
labels = ['cluster1','cluster2','cluster3','cluster4','cluster5','cluster6','cluster7']
Mean = df_mean['net_sale']
Count = df_p_count
Sum = df_p_sum
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, Count, width, label='% of Sales')
rects2 = ax.bar(x + width/2, Sum, width, label='% of Revenue')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Percent')
ax.set_title('Discount Promotion Analysis')
ax.set_xticks(x, labels)
#ax.set_xticklabels(labels)
ax.set_xticklabels(['cluster1','cluster2','cluster3','cluster4','cluster5','cluster6','cluster7'])
ax.legend()
fig.tight_layout()
plt.show()
Graph Output
您的代码无法生成正确可视化的原因是 ax.set_xticks(x, labels)
是错误的,因为 set_xticks
需要一个长度与 labels
相同的数字数组。
labels = ['cluster1','cluster2','cluster3','cluster4','cluster5','cluster6','cluster7']
# Mean = df_mean['net_sale']
# Count = df_p_count
# Sum = df_p_sum
Count = [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1]
Sum = [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, Count, width, label='% of Sales')
rects2 = ax.bar(x + width/2, Sum, width, label='% of Revenue')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Percent')
ax.set_title('Discount Promotion Analysis')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
fig.tight_layout()
plt.show()