使用位于父图外部的图例时的插图

时间:2018-02-16 17:50:30

标签: python matplotlib graph

我正在尝试按照接受的答案here插入父图表中的图表。

但是,我使用的绘图框架完全不同:

1)首先,图例位于图表之外,我已按照第here张贴的第二个答案,即缩小父图:

plt.figure()
# Shrink current axis by 20%
box = ax1.get_position()
ax1.set_position([box.x0, box.y0, box.width * 0.8, box.height])

2)对于标签,我使用的是ax1.legend((p1, p2, p3), ("label for p1", "label for p2", "label for p3"))程序。

如果我使用plt.show()sys.exit()评论运行代码,则会生成整个图表:

enter image description here

然而,当实现插入答案时,并试图插入粉红色和蓝色数据的区域:

(1)将plt.figure()替换为fig, ax1 = plt.subplots()

(2)使用ax2作为插图,因为已使用ax1将图例放置在图表外部,

(3)取消注释plt.show()sys.exit()

父图和插图中都没有绘制数据:

enter image description here

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import sys

x_1 =  np.array([ 2.56480648,  2.56635664, 2.57565757, 2.59425943, 2.61906191, 2.64463946,
  2.66711671, 2.69966997, 2.72292229, 2.75392539, 2.79422942, 2.81360636,
  2.84460946, 2.88026303, 2.91746675, 2.94846985, 2.99187419, 3.01822682,
  3.06085609, 3.08565857, 3.12286229, 3.18331833, 3.24067407, 3.25772577,
  3.36158616, 3.35616062, 3.43056806, 3.45847085, 3.61574815, 3.65387259,
  3.89764927, 9.1       ])


x_2 =  np.array([ 5.77982798, 5.77827783, 5.79067907, 5.81083108, 5.82788279, 5.85113511,
  5.87438744, 5.89763976, 5.92011701, 5.94414441, 5.96584658, 5.98987399,
  6.01467647, 6.03637864, 6.06118112, 6.08443344, 6.11543654, 6.13248825,
  6.16194119, 6.18751875, 6.21077108, 6.23479848, 6.26192619, 6.29137914,
  6.32083208, 6.35028503, 6.38128813, 6.41306631, 6.45569557, 6.47894789,
  6.50530053, 6.55645565, 6.5959846 , 6.62853785, 6.67349235, 6.71612161,
  6.76417642, 6.80293029, 6.86726173, 6.91841684, 6.96182118, 7.04165417,
  7.10908591, 7.23774877, 7.30208021, 9.40021502])


y_1 =  np.array([   0.,     10.,    30.1,  50.2,   70.3,   90.4,   110.51,  130.61,  150.71,
  170.81, 190.91, 211.01, 231.11, 251.21, 271.31, 291.41, 311.52, 331.62,
  351.72, 371.82, 391.92, 412.02, 432.12, 452.22, 472.32, 492.42, 512.53,
  532.63, 552.73, 572.83, 592.93, 592.93])


y_2 =  np.array([   0.,     10.,     30.1,    50.2,    70.3,    90.4,   110.51,  130.61,  150.71,
  170.81, 190.91, 211.01, 231.11, 251.21, 271.31, 291.41, 311.52, 331.62,
  351.72, 371.82, 391.92, 412.02, 432.12, 452.22, 472.32, 492.42, 512.53,
  532.63, 552.73, 572.83, 592.93, 613.03, 633.13, 653.23, 673.33, 693.43,
  713.54, 733.64, 753.74, 773.84, 793.94, 814.04, 834.14, 854.24, 874.34,
  894.44])

x_3 =  np.array([ 273.15, 323.15, 373.15, 423.15, 473.15])
x_4 =  np.array([ 295.16725084, 378.53216084, 476.23703528, 490.56204235])
y_3 =  np.array([ 1.4709975, 1.42196425, 1.372931  , 1.32389775, 1.2748645 ])
y_4 =  np.array([ 1.43766266, 1.46267139, 1.51159861, 1.52367087])


### Plotting:
plt.figure()
#fig, ax1 = plt.subplots()

p1  = plt.scatter(x_1, y_1, color='red', marker='o',  s=40)
p3 = plt.scatter(x_2, y_2, marker="o", color='black', facecolors='none', s=40)
p3c = plt.scatter(y_3, x_3, color='magenta', marker="o", facecolors='none', s=40)
p4  = plt.scatter(y_4, x_4, color='blue', marker='o',  s=40)

fontP = FontProperties()
fontP.set_size('12')

ax1 = plt.subplot(111)
# Shrink current axis by 20% in order to allow the legend to be outside the plot:
box = ax1.get_position()
ax1.set_position([box.x0, box.y0, box.width * 0.8, box.height])

ax1.legend((\
p1,\
p3,\
p3c,\
p4\
),\
(\
"1",\
"2",\
"3",\
"4"\
),\
prop=fontP, loc='center left', bbox_to_anchor=(1, 0.5))#  loc=4)
extraticks=[273.15+25]
plt.yticks(list(plt.yticks()[0]) + extraticks)
plt.gca().set_xlim(right=8)
plt.gca().set_ylim(bottom=-41.72, top=1040)
plt.grid()

plt.show()
sys.exit()

left, bottom, width, height = [0.25, 0.6, 0.2, 0.2]
ax2 = fig.add_axes([left, bottom, width, height])

p3 = ax2.scatter(x_2, y_2, color='black', marker="o", facecolors='none', s=40)
p3c = ax2.scatter(x_3, y_3, color='magenta', marker="o", facecolors='none', s=40)

ax2.set_xlim(right=2.26)
plt.gca().set_ylim(bottom=200, top=1040)
plt.grid()

plt.savefig('plot.pdf', bbox_inches='tight')


plt.show()

1 个答案:

答案 0 :(得分:1)

我认为在整个地方只有一些无用的命令使得代码产生2个数字而不是一个和多个子图而不是一个。插图的限制似乎也没有。删除所有这些将给你以下情节,这可能是你之后的。

### Plotting:
fig, ax1 = plt.subplots()

p1  = plt.scatter(x_1, y_1, color='red', marker='o',  s=40)
p3 = plt.scatter(x_2, y_2, marker="o", color='black', facecolors='none', s=40)
p3c = plt.scatter(y_3, x_3, color='magenta', marker="o", facecolors='none', s=40)
p4  = plt.scatter(y_4, x_4, color='blue', marker='o',  s=40)

fontP = FontProperties()
fontP.set_size('12')

box = ax1.get_position()
ax1.set_position([box.x0, box.y0, box.width * 0.8, box.height])

ax1.legend((p1,p3,p3c,p4),("1","2","3","4"),
           prop=fontP, loc='center left', bbox_to_anchor=(1, 0.5))

extraticks=[273.15+25]
plt.yticks(list(plt.yticks()[0]) + extraticks)
plt.gca().set_xlim(right=8)
plt.gca().set_ylim(bottom=-41.72, top=1040)
plt.grid()

left, bottom, width, height = [0.25, 0.6, 0.2, 0.2]
ax2 = fig.add_axes([left, bottom, width, height])

p3 = ax2.scatter(x_2, y_2, color='black', marker="o", facecolors='none', s=40)
p3c = ax2.scatter(x_3, y_3, color='magenta', marker="o", facecolors='none', s=40)

plt.grid()

plt.savefig('plot.pdf', bbox_inches='tight')
plt.show()

enter image description here