2017-04-01 72 views
0

请看下面的图表。在此图表中,我想绘制以(45,0)处的中心坐标为圆心。但是,如您所见,图表的下限为零,我的圈子的一半未显示。所以,我需要将这个图表扩展到底部。轴的“边距”方法(ax.margins())不起作用,因为此图的底线为零,零乘以任何数字等于零。Matplotlib边距

注意:请不要发布类似ax.set_ylim(...)或ax.set_yticks(...)的回复。我正在寻找这种类似问题的一般解决方案。

Code 
import matplotlib.pyplot as plt 
import numpy as np 

values = np.array([0.00388632352941, 0.00375827941176, 0.00355033823529,  0.00328273529412, 0.00294677941176, 0.00272142647059, 0.00246463235294,  0.00227766176471, 0.00213151470588, 0.00202594117647, 0.00183544117647,  0.00162102941177, 0.00148372058824, 0.00128380882353, 0.00112252941176,  0.000931544117647, 0.000786573529412, 0.000658220588235, 0.000584485294118,  0.000524044117647, 0.000562485294118, 0.000716441176471, 0.000872617647059,  0.00109039705882, 0.00124138235294, 0.00136894117647, 0.00143985294118,  0.00134760294118, 0.00121794117647, 0.00112772058824, 0.00109435294118,  0.00102432352941, 0.00101069117647, 0.00102417647059, 0.00104895588235,  0.00101776470588, 0.00101494117647, 0.000885558823529, 0.00078075,  0.000752647058824, 0.000667691176471, 0.000593220588236, 0.000658647058823,  0.000742117647059, 0.000651470588235, 0.000604647058824, 0.000584573529412,  0.00049530882353, 0.000281235294118, 0.000355029411765]) 
fig, ax = plt.subplots() 
ax.bar(np.arange(values.shape[0]), values) 
plt.show() 

enter image description here

+0

不知道你的圈子有多大。即如果它很小,就关闭那个圆的剪裁。即添加'clip_on = False'。 – klimaat

+0

我试过了,但没有结果。 –

回答

1

在问题没有圈,所以我的答案不包括任何圈子无论是。

为了在绘图中的数据周围留出一些空间,可以使用ax.margins(y=ymargin),其中ymargin是在数据的每一侧添加的空间的百分比。即如果数据从0到1并且您添加了边际的ymargin = 0.1,那么ylimits将是(-0.1, 1.1)。 (这与一个限制是否为零无关。)

现在,默认情况下,这不适用于条形图,因为它在一般情况下不希望将条形图启动到某处空气,而不是底部轴线。使用名为use_sticky_edges的标志来引导此行为。我们可以将此标志设置为False以恢复应用于轴两端的边界行为。为了生效,我们需要在之后致电ax.autoscale_view

import matplotlib.pyplot as plt 
import numpy as np 

values = np.array([3.89, 3.76, 3.55, 3.28, 2.95, 2.72, 2.46, 2.28, 2.13, 2.03, 1.84, 
        1.62, 1.48, 1.28, 1.12, 0.93, 0.79, 0.66, 0.58, 0.52, 0.56, 0.72, 
        0.87, 1.09, 1.24, 1.37, 1.44, 1.35, 1.22, 1.13, 1.09, 1.02, 1.01, 
        1.02, 1.05, 1.02, 1.01, 0.89, 0.78, 0.75, 0.67, 0.59, 0.66, 0.74, 
        0.65, 0.60, 0.58, 0.50, 0.28, 0.36]) 

fig, ax = plt.subplots() 
ax.bar(np.arange(values.shape[0]), values) 

ax.margins(y=0.3) 
ax.use_sticky_edges = False 
ax.autoscale_view(scaley=True) 
plt.show() 

enter image description here