資料繪制正常,但 x 軸上的旋轉標簽被裁剪。如何打開情節以使一切都適合?
def plot(data):
import matplotlib.pyplot as plt
# Uh oh. Our data is not what this logic expects. We need to break it into 2 lists
plt.style.use('ggplot')
breeds = [x[0] for x in data]
totals = [x[1] for x in data]
#
x_pos = [i for i, _ in enumerate(data)] # Figure out where the bars will go
plt.bar(x_pos, totals, color='green')
plt.xlabel("Breed")
plt.ylabel("Total Cows")
plt.title("Total Cows by Breed")
# We need to rotate the x axis labels to vertical because they are too long and they overlap
plt.xticks(rotation = 90)
plt.xticks(x_pos, breeds) # x_pos matches one-to-one with breeds
plt.show()
if __name__ == '__main__':
data = [["brown",100],["White",200], ["Zebra",4000], ["Unknown", 4500]]
plot(data)

uj5u.com熱心網友回復:
你可以使用緊湊的布局:
...
plt.tight_layout()
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383650.html
標籤:Python matplotlib
下一篇:帶有日期的Pyplot堆疊條形圖
