如何使用matplotlib生成阴影区域

时间:2016-10-29 23:59:34

标签: python matplotlib

我试图在这个paper中得到数字5,但是我无法生成两个阴影区域,表明错误为1和2千卡/摩尔。每个阴影区域必须使用与纸张不同的颜色。我怎样才能做到这一点?

我的代码:

#!/usr/bin/python
import numpy as np
import pylab as plot
import matplotlib.pyplot as plt
import numpy, scipy, pylab, random
from matplotlib.ticker import MultipleLocator
import matplotlib as mpl
from matplotlib.ticker import MaxNLocator

color = "#252525"
gray = "#777777"

with open("input.txt", "r") as f:
    x=[]
    y=[]
    z=[]

    for line in f:
        if not line.strip() or line.startswith('@') or line.startswith('#'): continue
        row = line.split()
        x.append(float(row[0]))
        y.append(float(row[1]))
        z.append(float(row[2]))


fig = plt.figure(figsize=(3.2,2.2), dpi=300)
ax = plt.subplot(111)


plt.xlim(-2, -12)
plt.ylim(-2, -12)
ax.xaxis.set_major_locator(MaxNLocator(6))
ax.yaxis.set_major_locator(MaxNLocator(6))

ax.xaxis.set_minor_locator(MultipleLocator(1))
ax.yaxis.set_minor_locator(MultipleLocator(1))

plt.plot(x,y,'o',color="black", ms=3, mec="black")
plt.plot(x,z, color='gray' , linestyle = "dashed", dashes=(2,2))

plt.xlabel('Experimental (kcal/mol)', fontsize=8)
plt.ylabel('Predicted (kcal/mol)', fontsize=8)

for axis in ['top','bottom','left','right']:
  ax.spines[axis].set_linewidth(0.5)

plt.subplots_adjust(top=0.95)
plt.subplots_adjust(bottom=0.18)
plt.subplots_adjust(left=0.14)
plt.subplots_adjust(right=0.95)


plt.tick_params(axis='both', which='major', labelsize=7)
plt.tick_params(axis='both', which='minor', labelsize=0)

plt.savefig("output.png", dpi=300)

输入文件:

-2      -2.0    -2
-3      -3.5    -3
-4      -5.0    -4
-5      -7.0    -5
-6      -6.0    -6
-8      -10.1   -8
-10     -10.0   -10
-12     -12.0   -12

获得的输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

您正在寻找plt.fill_between。您可以在绘图中添加以下内容:

plt.plot([-2,-12],[-2,-12], color='gray', linestyle = "dashed", dashes=(2,2))
plt.fill_between([-2,-12], [-3,-13], [-1,-11], color='lightblue', alpha=0.5)
plt.fill_between([-2,-12], [-4,-14], [0,-10], color='lightblue', alpha=0.35)

将创建两个填充区域。