简体   繁体   English

在python中绘制具有不同背景颜色的曲线

[英]Plotting a curve with different background colors in python

I am trying to plot a curve after reading a data file. 我试图在读取数据文件后绘制曲线。 The background above the curve and below the curve should be different (say, red and green). 曲线上方和曲线下方的背景应该不同(例如,红色和绿色)。 This is like a phase diagram showing two different phases above and below a boundary line. 这就像一个相图,显示了边界线上下两个不同的相位。 Is it possible using matplotlib in python? 可以在python中使用matplotlib吗? My code is 我的代码是

#!/usr/bin/env python

import numpy as np
import pylab as pl
import matplotlib.pyplot as plt

# Use numpy to load the data contained in the file
# 'data.txt' into a 2-D array called data
data = np.loadtxt('data.txt')
# plot the first column as x, and second column as y
x=data[:,0]
y=data[:,1]
pl.plot(x,y, '-r')
pl.xlabel('x')
pl.ylabel('y')
pl.show()

The 'data.txt' file contains : 'data.txt'文件包含:

0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81

So it should plot a parabola. 所以它应该绘制一个抛物线。

There is a possible duplicate here . 有可能重复这里 However, that doesn't seem to work in my case. 但是,这似乎不适用于我的情况。

Also I would like enter texts in the middle of the differently colored patches. 另外,我想在不同颜色的补丁中间输入文本。

So my plot should look like: 所以我的情节应该是这样的:

在此输入图像描述

Yes, this is possible using matplotlib's fill_between. 是的,这可以使用matplotlib的fill_between。 You can fill below the line simply with plt.fill_between(x,y,color="whatever") . 您可以使用plt.fill_between(x,y,color="whatever")简单地填充该行。 To be more complete, you can set the lower bound of the fill with plt.fill_between(x,0,y,color="whatever") 为了更完整,您可以使用plt.fill_between(x,0,y,color="whatever")设置填充的下限

To fill the whole area above the curve, you can use plt.fill_between(x,y,plt.ylim()[1],color="whatever") 要填充曲线上方的整个区域,可以使用plt.fill_between(x,y,plt.ylim()[1],color="whatever")

Here is some demo code that works to fill both above and below the curve: 以下是一些可以填充曲线上方和下方的演示代码:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,9,10)
y = x ** 2
plt.plot(x,y,"b-")
plt.fill_between(x,y,color="red")
maxy = plt.ylim()[1]
plt.fill_between(x,y,maxy,color="green")
plt.show()

You can set x and y however you want. 您可以根据需要设置x和y。

Hope this helps! 希望这可以帮助!

EDIT: You can add text with matplotlib's text. 编辑:您可以使用matplotlib的文本添加文本。 You need to specify the coordinates, but you could do something like 你需要指定坐标,但你可以做类似的事情

xtextplace = x[7]
ytextplace = y[7] / 2
plt.text(xtextplace,ytextplace,"Some Text")

I'd do something like: 我会这样做:

xcoord = x[int((x.size/3)*2)]
ycoord = y[int((x.size/3)*2)] / 2
plt.text(xcoord,ycoord,"Text")

This would make the text appear 2/3 of the way along the x-axis and 1/2 way up under the curve at that point. 这将使文本在x轴上显示为2/3,在该点处显示为曲线下方的1/2。

I did some corrections to your code. 我对你的代码做了一些修改。 The below code works fine(with the same "data.txt") 下面的代码工作正常(使用相同的“data.txt”)

Python3 version: Python3版本:

#!/usr/bin/python

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('data.txt')
x=data[:,0]
y=data[:,1]
(ax1, ax2) = plt.subplots(sharex=True, sharey=True)
ax2.fill_between(x, y, facecolor='green')
ax2.fill_between(x, y, plt.ylim()[1], facecolor='red')
plt.show()

Output: 输出:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM