簡體   English   中英

Matplotlib Pyplot條形圖中的浮動條

[英]Floating Bars in Matplotlib Pyplot Bar Graph

我正在處理一個范圍非常大的圖形,發現條形圖偏離了軸。 我該如何阻止呢? 這是我在本教程中編寫的一個示例,因為我的數據是從讀取大量文件中獲得的。 這說明了我遇到的問題。 我給了stds並意味着很大的范圍,就像我的數據一樣。

import numpy as np
import matplotlib.pyplot as plt

N = 5
menMeans = (20, 35, 30, 35, 27,43,444,44)
menStd =   (2, 3, 4, 1, 2,43,4,33)

ind = np.arange(8)  # the x locations for the groups
width = 0.35       # the width of the bars

fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)

womenMeans = (25, 32, 34, 25,322,3,443,3)
womenStd =   (3, 5, 2, 3, 3,32,33,322)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)

# add some
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5','K','K','Z') )

ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )

def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

plt.show()

只需將限制添加到y軸:

import numpy as np
import matplotlib.pyplot as plt

N = 5
menMeans = (20, 35, 30, 35, 27,43,444,44)
menStd =   (2, 3, 4, 1, 2,43,4,33)

ind = np.arange(8)  # the x locations for the groups
width = 0.35       # the width of the bars

fig = plt.figure()
ax = fig.add_subplot(111)
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)

womenMeans = (25, 32, 34, 25,322,3,443,3)
womenStd =   (3, 5, 2, 3, 3,32,33,322)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)

# add some
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5','K','K','Z') )

ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
ax.set_ylim(0)

def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

plt.show()

ax.set_ylim(0)可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM