我用下面的代码在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.tight_layout()
和/或
bbox_inches='tight'
最明显的一个问题是缩放字体的大小,至少在这个字体的大小上。我认为整个twinx()
的事情使一切变得非常不稳定,但对我这个新手来说,这似乎是一个比this更简单的解决方案。阅读大量类似这样的问题来寻找答案似乎没有“只需手动在底部添加一些空间而不缩放”这样简单的方法,但我还是要尝试去问。
有没有办法将图例放在图形的最底部,并保持图形的字体大小和外观不变,即不使用任何缩放命令?在
使用
其中“lower”表示锚定点的y坐标(0.)表示图例的下边框,“center”表示x坐标(0.5)表示图例的中心。两个坐标的范围都是0到1。如How to specify legend position in matplotlib in graph coordinates所示
我现在遵循了example的原则,通过一些{a2}我可以得到我想要的结果。以下是图例位置正确的代码:
下一次我会更仔细地看看画廊。非常感谢您的时间!在
相关问题 更多 >
编程相关推荐