[英]Long vertical bar plot with matplotlib
我需要創建一個Python腳本,將(排序的)值列表繪制為垂直條形圖。 我想繪制所有值並將其保存為較長的垂直圖,以便yticks標簽和條都清晰可見。 也就是說,我想要一個“長”垂直圖。 列表中的元素數量有所不同(例如,從500到1000),因此使用figsize
並沒有幫助,因為我不知道應該多長時間。
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
# Example data
n = 500
y_pos = np.arange(n)
performance = 3 + 10 * np.random.rand(n)
ax.barh(y_pos, np.sort(performance), align='center', color='green', ecolor='black')
ax.set_yticks(y_pos)
ax.set_yticklabels([str(x) for x in y_pos])
ax.set_xlabel('X')
如何修改腳本,以便可以垂直拉伸圖形並使之可讀?
根據數據值的數量更改figsize
。 另外,相應地管理y軸限制。
以下內容非常適用:
n = 500
fig, ax = plt.subplots(figsize=(5,n//5)) # Changing figsize depending upon data
# Example data
y_pos = np.arange(n)
performance = 3 + 10 * np.random.rand(n)
ax.barh(y_pos, np.sort(performance), align='center', color='green', ecolor='black')
ax.set_yticks(y_pos)
ax.set_yticklabels([str(x) for x in y_pos])
ax.set_xlabel('X')
ax.set_ylim(0, n) # Manage y-axis properly
下面給出的是n=200
的輸出圖片
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.