我正在通过在 Python 中使用 pandas 从 CSV 读取数据来绘制条形图。我将 CSV 读入 DataFrame
并使用 matplotlib 绘制它们。
这是我的 CSV 的样子:
SegmentName Sample1 Sample2 Sample3
Loop1 100 100 100
Loop2 100 100 100
res = DataFrame(pd.read_csv("results.csv", index_col="SegmentName"))
我绘制并将图例设置在外面。
plt.figure()
ax = res.plot(kind='bar')
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.savefig("results.jpg")
但是,x 轴刻度标签是垂直方向的,因此我无法阅读文本。我外面的传说也被切断了。
我能否将刻度标签的方向更改为水平方向,然后调整整个图形以使图例可见?
最佳答案
设置标签时尝试使用“旋转”关键字。例如:
plt.xlabel('hi',rotation=90)
或者如果您需要旋转刻度标签,请尝试:
plt.xticks(rotation=90)
至于图例的定位等,大概值得看看tight layout guide
关于python - 更改绘图的标记方向和图例位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18098061/