如何使用matplotlib生成着色区域

2024-04-25 21:57:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在这个paper中得到图5,但我无法生成两个阴影区域,它们指示1和2 kcal/mol的误差。每个阴影区域必须与纸张中的颜色不同。我怎么能做到呢?在

#!/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)

输入文件:

^{pr2}$

获得的输出:

enter image description here


Tags: importplotmatplotlibaslinepltaxrow
1条回答
网友
1楼 · 发布于 2024-04-25 21:57:10

您正在查找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)

这将创建两个填充区域。在

相关问题 更多 >