Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在matplotlib中自动调整字体大小_Python_Python 2.7_Matplotlib_Font Size - Fatal编程技术网

Python 在matplotlib中自动调整字体大小

Python 在matplotlib中自动调整字体大小,python,python-2.7,matplotlib,font-size,Python,Python 2.7,Matplotlib,Font Size,这是我在网站上的第一个问题,所以请让我知道它是否应该更详细或问不同的问题 我使用的是python 2.7和matplotlib 2.0.0 基本上,我在做一个matplotib条形图,我希望y值在条形图上方或之中。这是一个反复出现的问题,使用之前的所有答案,我已经能够编写一个“自动标签”功能,该功能运行得非常好。然而,有一个问题我无法解决。此图表是自动生成的大型报告的一部分,时间范围可能会更改。所以可能是一周,一个月或者一年。所以我只能有几百个这样的酒吧。 当我将fig保存为png时,为了能够放

这是我在网站上的第一个问题,所以请让我知道它是否应该更详细或问不同的问题

我使用的是python 2.7和matplotlib 2.0.0

基本上,我在做一个matplotib条形图,我希望y值在条形图上方或之中。这是一个反复出现的问题,使用之前的所有答案,我已经能够编写一个“自动标签”功能,该功能运行得非常好。然而,有一个问题我无法解决。此图表是自动生成的大型报告的一部分,时间范围可能会更改。所以可能是一周,一个月或者一年。所以我只能有几百个这样的酒吧。 当我将fig保存为png时,为了能够放大并读取我需要的所有内容,我使用了15*15的fig大小和dpi=1200

所以我的问题是,根据条的数量,条的宽度会改变,字体大小也会改变。我设法让它在22天内工作得很好,但当我更改日期时,它并不适应,即使我的字体大小是天数(因此是条数)的函数。我将给出绘制条形图的代码,以及自动标签功能。该项目是大的,我不能把整个代码,所以让我知道,如果你需要更多的信息。我只为一个rect校准自动标签功能,但最终我会在它工作后全部校准

fig2, (ax21, ax22) =plt.subplots(2, sharex=True)
fig2.set_size_inches(15, 15)
ind2=ind.tolist()


### Stuffs with the first subplots###

width=0.1
rects21=ax22.bar(ind-3*width, TotDelta, color='#620DB7', width=width, align='center', label='Actual Delta')
check.autolabel(rects21, ax22, width) ### THE AUTOLABEL FUNCTION
rects22=ax22.bar(ind-2*width, delta, color='r',width=width, align='center', label='Model Delta')
rects23=ax22.bar(ind-width, DeltaExp, color='c', width=width, align='center', label='Delta Explained')
rects24=ax22.bar(ind, fees, color='b', width=width, align='center', label='Fees')
rects25=ax22.bar(ind+width, slip, color='#F0F304', width=width, align='center', label='Slippage')
rects26=ax3.bar(ind+2*width, other, color='g', width=width, align='center', label='Other Unamtched')
rects27=ax3.bar(ind+3*width, unexp, color='#692C2C', width=width, align='center', label='Unexplained Delta')
ax22.axhline(linewidth=1, color='k')
ax22.xaxis.set_major_formatter(ticker.FuncFormatter(check.date_format))
ax22.grid()
fontP = FontProperties()
fontP.set_size('small')
ax22.legend(loc='center left', prop=fontP, bbox_to_anchor=(1, 0.5))

plt.xticks(rotation=45)
fig2.tight_layout()
fig2.subplots_adjust(top=0.95, right=0.85)
fig2.suptitle('All Trades')
fig2.savefig("/Users/davidabitbol/Documents/all_dollar.png", dpi=1200)
自动标签功能:

def autolabel(rects, ax, width):
#To be used with a 15*15 fig size and 1200 dpi png
(y_bottom, y_top) = ax.get_ylim()
y_height = y_top - y_bottom

for rect in rects:
    height = rect.get_height()
    y=rect.get_y()
    height
    if height != 0:

        if y>=0: # Positive value, we use the height of the bar, y value = 0 for some reasons
            p_height=height/y_top

            if p_height > 0.8: #If more thna 0.8 then we write the text in the bar, as there is not enough place above or below it.
                label_position = height - (y_height * 0.05)
            else:
                label_position = height + (y_height * 0.01)

            ax.text(rect.get_x() + rect.get_width()/2., label_position,
                    '%.3f' % height,
                    ha='center', va='bottom', size=(22*width)/(10*len(rects)))

        else: # if negative we use the y value since the heing is positivie
            p_height=y/y_bottom

            if p_height > 0.8:
                label_position = y + (y_height * 0.05)
            else:
                label_position = y - (y_height * 0.01)

            ax.text(rect.get_x() + rect.get_width()/2., label_position,
            '%.3f' % y,
            ha='center', va='bottom', size=(22*width)/(10*len(rects)))

任何人都有这样的想法,即文本的大小或多或少与保存图像中的条的大小相同?您是否考虑过在增加或减少图形宽度以适应所需条数的同时保持条的宽度和字体大小不变?如果您必须放大以读取文本大小,您最好保持文本大小不变,只需滚动条形图即可。您可以缩小以获得完整图表的透视图,就像您所描述的那样。@Brian谢谢,对不起,我刚才看到了您的评论。我不在我的电脑上了。你能更具体地说明你的建议吗?如果我理解正确,您希望我根据钢筋数量更改fig_尺寸。这样做的问题是,图例、标题和边距的位置也应该调整……不用担心。是的,我建议更改
fig_size
参数。图例、标题和页边距的位置通常根据对象作为一个整体来指定(例如,
bbox\u to\u anchor=(1,0.5)
指定图例应位于轴右侧的中心,而不考虑绝对大小)。如果需要特定的帮助(如实际代码)请用虚拟数据分享一个简化的功能图示例,以便我们重现问题。有关指导信息,请参阅堆栈溢出文档:。