传奇被切断,解决问题是以不受欢迎的方式影响字体大小

时间:2017-10-12 14:53:18

标签: python matplotlib

我正在使用以下代码在Matplotlib中创建一个图形,我希望它不会太乱:

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import locale
locale.setlocale(locale.LC_ALL, 'German')

#the data to be plotted
CSB = [9205.0, 8845.0, 19740.0, 410.0, 11560.0, 11632.0, 14368.0,
       11556.0, 9846.0, 14544.0]
DOC = [np.nan, 1853.0, 4172.0, 259.0, np.nan, np.nan, np.nan, np.nan,
       np.nan, np.nan]
NH3N = [3593.5, 3318.8, 5208.0, 306.4, 2708.2, 2682.1, 2812.3, 3033.1,
        3098.4, 3815.9]
x = np.linspace(1, 10, 10)
Daten = ['09.05.2017', '16.05.2017', '23.05.2017', '06.06.2017', '28.08.2017',
    '31.08.2017', '04.09.2017', '07.09.2017', '14.09.2017', '18.09.2017']

#setting the font and font size globally
font = {'family' : 'Arial',
        'size'   : 12}
matplotlib.rc('font', **font)

fig, ax1 = plt.subplots()

#first plot
l1, = ax1.plot(x, NH3N, 'k+', label='NH$_3$-N-Konzentration', ms=8)
ax1.set_ylabel(r'NH$_3$-N-Konzentration $[\frac{mg}{L}]$')
ax1.set_xlabel('Datum Probenahme')
ax1.set_ylim([0,10000])
ax1.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x,
             loc: "{0:n}".format(float(x))))

#getting the x tick lables fitted to dates
plt.xticks(x, Daten, rotation=30)
fig.autofmt_xdate()
plt.subplots_adjust(bottom=0.3)

#second plot as a parasite of the first
ax2 = ax1.twinx()
l2, = ax2.plot(x, CSB, 'k.', label='CSB-Konzentration', ms=8)
ax2.set_ylabel(r'CSB-Konzentration $[\frac{mg}{L}]$')
ax2.set_ylim([0,25000])
ax2.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x,
             loc: "{0:n}".format(float(x))))

#third plot as a parasite of the first
ax3 = ax1.twinx()
l3, = ax3.plot(x, DOC, 'k^', label='DOC-Konzentration', ms=8)
ax3.set_ylabel(r'DOC-Konzentration $[\frac{mg}{L}]$')
ax3.set_ylim([0,5000])
ax3.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x,
             loc: "{0:n}".format(float(x))))

#manipulating the position of the third y axis
ax3.tick_params(direction='in', pad=-50)
ax3.spines['left'].set_position(('axes', 1.3))
ax3.yaxis.set_ticks_position('left')
ax3.yaxis.set_label_coords(1.48,0.5)

fig.legend((l1,l2,l3),('NH$_3$-N-Konzentration','CSB-Konzentration',
           'DOC-Konzentration'), loc=9, ncol = 3, bbox_to_anchor=(0.75, 0.15))

plt.savefig('CSB_NH3N_DOC_unbehandeltes_Abwasser.eps', format='eps',
            dpi=1000, bbox_inches='tight')

plt.show()

代码产生了这个graph,它应该是Latex文件的一部分。我对Python很陌生,所以我可能忽略了一些问题,或者可能还有其他问题,除了我要求的问题:我很抱歉。 现在我的问题是:图例应该低于图,我可以用bbox_to_anchor属性完成。这会产生一个截止的图例,这是我试图用

解决的问题
plt.margins(0.2)
plt.subplots_adjust(bottom=0.30)

和/或

plt.tight_layout()

和/或

bbox_inches='tight'

最后,所有这些都是以笨拙的方式缩放孔图或者至少缩放字体大小,这个问题在将图形添加到Latex文件时变得明显。我认为整个twinx()事情使一切变得非常不稳定,但对我来说,作为一个新手,它似乎比this更容易解决。阅读很多像这样的问题来找到答案似乎没有简单的方法,比如“只需手动向底部添加一些空间而不进行缩放”,但无论如何我都要试着去问。
有没有办法将图例放在图的最底部,并保持字体大小和图形的外观,即没有任何缩放命令?

2 个答案:

答案 0 :(得分:0)

使用

loc="lower center", bbox_to_anchor=(0.5, 0.)

其中“lower”表示锚点的y坐标(0。)表示图例的下边框,而“中心”表示x坐标(0.5)表示图例的中心。两个坐标的范围都是0到1.这在How to specify legend position in matplotlib in graph coordinates

中有说明

答案 1 :(得分:0)

我现在跟随将军example和一些help我能够达到我所针对的结果。以下是具有正确定位图例的代码:

import matplotlib.pyplot as plt
import numpy as np

CSB = [9205.0, 8845.0, 19740.0, 410.0, 11560.0, 11632.0, 14368.0,
       11556.0, 9846.0, 14544.0]
DOC = [np.nan, 1853.0, 4172.0, 259.0, np.nan, np.nan, np.nan, np.nan,
       np.nan, np.nan]
NH3N = [3593.5, 3318.8, 5208.0, 306.4, 2708.2, 2682.1, 2812.3, 3033.1,
        3098.4, 3815.9]
x = np.linspace(1, 10, 10)
Daten = ['09.05.2017', '16.05.2017', '23.05.2017', '06.06.2017', '28.08.2017',
    '31.08.2017', '04.09.2017', '07.09.2017', '14.09.2017', '18.09.2017']

font = {'family' : 'Arial',
        'size'   : 12}
plt.rc('font', **font)

fig = plt.figure()
host = fig.add_subplot(111)

par1 = host.twinx()
par2 = host.twinx()

host.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x,
             loc: "{0:n}".format(float(x))))
par1.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x,
             loc: "{0:n}".format(float(x))))
par2.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x,
             loc: "{0:n}".format(float(x))))

offset = 85
par2.spines['right'].set_position(('outward', offset))  

host.set_xlim(0, 11)
host.set_ylim(0, 10000)

host.set_xlabel("Datum Probenahme")
host.set_ylabel(r'NH$_3$-N-Konzentration $[\frac{mg}{L}]$')
par1.set_ylabel(r'CSB-Konzentration $[\frac{mg}{L}]$')
par2.set_ylabel(r'DOC-Konzentration $[\frac{mg}{L}]$')

host.set_xticks(x)
host.set_xticklabels(Daten)


p1, = host.plot(x, NH3N, '+k', label='NH$_3$-N-Konzentration')
p2, = par1.plot(x, CSB, '.k', label=r'CSB-Konzentration')
p3, = par2.plot(x, DOC, '^k', label=r'DOC-Konzentration')

par1.set_ylim(0, 25000)
par2.set_ylim(0, 5000)

host.legend((p1,p2,p3),('NH$_3$-N-Konzentration','CSB-Konzentration','DOC-Konzentration'), loc='lower center', ncol = 3, bbox_to_anchor=(0.5, -0.5))

fig.autofmt_xdate()
plt.tight_layout()
plt.show()

下次我会仔细看看画廊。非常感谢你的时间!