我在类似内容上经历过一堆关于SO的问题并阅读以理解它的解剖结构仍然无法找到解决方案如何旋转标签。其他来源Link1 Link2 { {3}} 以下是我的努力
fig, ax = plt.subplots()
# Although I have commented out this part it would be great to know what is wrong with it
#xaxis = myfiledic.keys()
#yaxis = myfiledic.values()
#ax.plot(xaxis, yaxis)
plt.bar(range(len(myfiledic)), myfiledic.values())
plt.xticks(range(len(myfiledic)), myfiledic.keys())
for line in ax.xaxis.get_majorticklabels():
print line
ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=45)
plt.show()
打印行指向输出以下。我只想尝试用C代码和行数绘制一些文件。所以xlabel是我期待的文件名,下面是我得到的
Text(0,0,'add_2_arrays.c')
Text(0,0,'bst.c')
Text(0,0,'sort_string_array.c')
Text(0,0,'palindrome_int.c')
答案 0 :(得分:0)
我不确定我明白你的问题是什么。当我运行你的代码时,我会得到一个带有旋转标签的条形图,就像你要求的那样。
这里的代码包含一些编辑内容,但这只是一个最小的工作示例:
请注意,我在plt.bar命令中添加了import matplotlib.pyplot as plt
myfiledic = {
'add_2_arrays.c': 1000,
'bst.c': 1024,
'sort_string_array.c': 42,
'palindrome_int.c': 666,
}
fig, ax = plt.subplots()
plt.bar(range(len(myfiledic)), myfiledic.values(), align='center')
plt.xticks(range(len(myfiledic)), myfiledic.keys())
for line in ax.xaxis.get_majorticklabels():
print line
ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=45)
# Make more room for the labels
plt.subplots_adjust(bottom=0.25)
plt.show()
,以使条形图以刻度线为中心而不是左对齐。
from collections import OrderedDict
请注意,从dict获取数据的顺序是任意的,因此如果您需要按特定顺序获取数字,最好使用OrderedDict:# Although I have commented out this part it would be great to know what is wrong with it
#xaxis = myfiledic.keys()
#yaxis = myfiledic.values()
#ax.plot(xaxis, yaxis)
然后放入数据按照你想要阅读它的顺序进入它。
至于这一点:
In [44]: xaxis
Out[44]: ['sort_string_array.c', 'sort_string_array.cS', 'bst.c', 'add_2_arrays.c']
In [45]: yaxis
Out[45]: [42, 666, 1024, 1000]
xaxis是一个字符串列表是错误的:
PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: rsa-key-20160508
Public-Lines: 12
绘图(x,y)函数根据x和y中的数字在轴中放置点。字符串' sort_string_array.c'不是一个数字,所以在plot()命令中使用它是没有意义的。