我似乎无法在matplotlib中获得子图的键间距。我希望x轴上的按键排列到条形图的位置。在某些情节中,y轴上的标记也会以一种奇怪的方式被微调,我似乎无法找到解决方案。以下是为参考而生成的代码和图表。
EX. {'daniel': [#shares, #views, #comments, #likes, #total_engagement]}
def plotUserEngagement():
keys = top_users.keys()
f, metrics = plt.subplots(3, 2)
for graph_data in [(0, 0, 'shares', 0), (0, 1, 'views', 1), (1, 0, 'comments', 2), (1, 1, 'likes', 3), (2, 0, 'Total Engagement', 3)]:
x = graph_data[0]
y = graph_data[1]
engagement_metric = graph_data[2]
index = graph_data[3]
graph = metrics[x, y]
graph.set_title('User ' + engagement_metric)
user_metric_data = [engagement_by_user[user][index] for user in top_users]
graph.bar(range(len(top_users)), user_metric_data)
if [x,y] == [2,0]:
graph.set_xticklabels(keys, rotation = 'vertical')
metrics[2,1].set_xticklabels(keys, rotation = 'vertical')
f.tight_layout()
在上面的示例中,我希望在底部x轴上看到10个用户名,每个10个条线都有一个。我验证了这些键'变量实际上包含10个项目。
我有类似的问题在下面的图表中沿着x轴显示表情符号,我期待10个表情符号,每个栏一个: keys = [emoji.decode(' utf-8')for top_emojis中的表情符号] f,metrics = plt.subplots(2,2)
def plotEmojiData():
for graph_data in [(0, 0, 'Appearances', 0), (0, 1, 'Comments', 1), (1, 0, 'Likes', 2)]:
x = graph_data[0]
y = graph_data[1]
emoji_characterictic = graph_data[2]
index = graph_data[3]
graph = metrics[x, y]
graph.set_title('Emoji ' + emoji_characterictic)
characteristic_data = [top_emojis[emoji][index] for emoji in top_emojis]
graph.bar(range(len(top_emojis)), characteristic_data)
graph.set_xticklabels(keys, fontname='symbola', fontsize=20)
plotEmojiData()
engagement_graph = metrics[1, 1]
engagement_graph.set_title('Emoji Total Engagement')
totalEngagement = [appearances[i]+comments[i]+likes[i] for i in range(len(top_emojis))]
engagement_graph.bar(range(len(top_emojis)), totalEngagement)
engagement_graph.set_xticklabels(keys, fontname='symbola', fontsize=20)
f.tight_layout()
plt.show()
答案 0 :(得分:0)
如果您希望显示所有标签,则需要明确设置正确的刻度数:
graph.bar(range(len(top_users)), user_metric_data)
graph.set_xticks(range(len(top_users)) # <<<<<<<<<<<<<<<<<<<<<<<
graph.set_xticklabels(keys, rotation = 'vertical')