创建Matplotlib图,使用Python中的子图通用 [英] Creating Matplotlib Graph with common line across subplots in Python

查看:217
本文介绍了创建Matplotlib图,使用Python中的子图通用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于即将到来的作业,我需要制作一系列图表,其中有两个图表,其中有一条线条从一个图表转到另一个图表,并在另一个图表上对该线条下方的区域着色。



如下图所示:


这就是我现在所拥有的:

<$>

<$>

从matplotlib导入pyplot为plt

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.plot ([0,1,2,3,4,5,6,7,8,9,10],[1,1,1,1,1,0,0,0,0,0,0],' -b')
ax1.plot([0,1,2,3,4,5,6,7,8,9,10],[0,0,0,0,0,1,1 ,1,1,1,1],'-r')
ax1.set_ylim([0,1.2])

ax2 = fig.add_subplot(122)
ax2 .plot([0,5,10,15,20,25,30,35,40],[1,1,1,1,0,0,0,0,0],'-b')
ax2.plot([0,5,10,15,20,25,30,35,40],[0,0,0,0,1,1,1, 1,1],'-r')
ax2.set_ylim([0,1.2])

plt.show()

很明显,这只会生成两个图表,我还无法在两个图表中添加该行。



我真的希望能够在python中使用Matplotlib来完成此操作,并且可以更改该值(示例中为45),并自动更改颜色区域。



感谢!

解决方案

有三个步骤:第一步,在左侧面板中找到绿线和蓝线之间的截取点。第二,在右侧面板中找到红色和线条之间的截取点。第三,填写之间的区域。这些步骤涉及 np.interp scipy.interpolat scipy.optimize plt.fill_between ,您应该查看。

  from matplotlib import pyplot as plt 
import numpy as np
import scipy.interpolate as spinp
import scipy.optimize as spop

fig = plt.figure(figsize =( 16,4))
ax1 = fig.add_subplot(121)
x = [0,10,20,30,40,50,60,70,80,90,100]
yr = [ 1,1,1,1,1,0,0,0,0,0,0]
yg = [0,0,0,0,0,1,1,1,1,1,1,1 ]
turn_pt = np.interp(45,x,yr)#将45改成任意值。
yb = [0.,turn_pt,turn_pt]
ax1.plot(x,yr,'-r')
ax1.plot(x,yg,'-g')
xb = [45,45,200]
ll = plt.plot(xb,yb,'-b')
ll [0] .set_clip_on(False)
plt.axis ([0,100,0,1.2])
#上述三行可以从盒子中划出线条。

ax2 = fig.add_subplot(122)
yr = [1,1,1,1,0,0,0,0,0]
yg = [0, 0,0,0,1,1,1,1,1]
x = [0,5,10,15,20,25,30,35,40]
brk_pt_f =λX,V :(spinp.interp1d(x,yr)(X)-V)** 2
brk_pt = spop.fmin(brk_pt_f,17.,args =(turn_pt,),disp = 0)#17。是你的初步猜测,
#上述两条线解决了蓝线和红线之间的交点
zero_pt = 20。
start_pt = 0.
xb = np。 hstack((start_pt,brk_pt,zero_pt))
yb = [turn_pt,turn_pt,0]
ax2.plot(x,yr,'-r')
ax2.plot(x, yg,'-g')
ax2.plot(xb,yb,'-b')
ax2.hlines(turn_pt,0,40,'b',alpha = 0。)
ax2.fill_between(xb,yb,0,alpha = 0.4)
ax2.set_ylim([0,1.2])
ax2.set_xlim([0,40])



有几个解决方案可以摆脱顶部x轴和右侧y轴,请搜索旧的SO帖子。



最后,欢迎来到SO。

For an upcoming assignment I am require to make a series of diagrams that have two graphs that have a line going across from one graph to the other, colouring an area below that line on the other graph.

As shown in this rough drawing:

This is what I currently have:

From this code:

from matplotlib import pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.plot([0,1,2,3,4,5,6,7,8,9,10], [1,1,1,1,1,0,0,0,0,0,0], '-b')
ax1.plot([0,1,2,3,4,5,6,7,8,9,10], [0,0,0,0,0,1,1,1,1,1,1], '-r')
ax1.set_ylim([0, 1.2])

ax2 = fig.add_subplot(122)
ax2.plot([0,5,10,15,20,25,30,35,40], [1,1,1,1,0,0,0,0,0], '-b')
ax2.plot([0,5,10,15,20,25,30,35,40], [0,0,0,0,1,1,1,1,1], '-r')
ax2.set_ylim([0, 1.2])

plt.show()

Obviously this only generates the two graphs and I have yet been unable to add the line across the two graphs.

I really want to be able to do this with Matplotlib in python with the ability to change the value (45 in the example case) and the coloured area change automatically.

Thanks!

解决方案

There are three steps: 1st, find the intercept point between green and blue lines in the left panel. 2nd, find the intercept point between the red and lines in the right panel. 3rd, fill the area between. These steps involves np.interp scipy.interpolat scipy.optimize and plt.fill_between, which you should look up.

from matplotlib import pyplot as plt
import numpy as np
import scipy.interpolate as spinp
import scipy.optimize as spop

fig = plt.figure(figsize=(16,4))
ax1 = fig.add_subplot(121)
x  = [0,10,20,30,40,50,60,70,80,90,100]
yr = [1,1,1,1,1,0,0,0,0,0,0]
yg = [0,0,0,0,0,1,1,1,1,1,1]
turn_pt = np.interp(45, x, yr) #change 45 to whatever.
yb = [0.,turn_pt,turn_pt]
ax1.plot(x, yr, '-r')
ax1.plot(x, yg, '-g')
xb = [45, 45, 200]
ll = plt.plot(xb,yb, '-b')
ll[0].set_clip_on(False) 
plt.axis([0,100,0,1.2])
#the above three lines to draw the line out of the box.

ax2 = fig.add_subplot(122)
yr = [1,1,1,1,0,0,0,0,0]
yg = [0,0,0,0,1,1,1,1,1]
x  = [0,5,10,15,20,25,30,35,40]
brk_pt_f = lambda X, V: (spinp.interp1d(x, yr)(X)-V)**2
brk_pt = spop.fmin(brk_pt_f, 17., args=(turn_pt,), disp=0) #17. is you inital guess,
#the above two lines solve for the intersection between the blue line and the red line
zero_pt = 20.
start_pt= 0.
xb = np.hstack((start_pt, brk_pt, zero_pt))
yb = [turn_pt,turn_pt,0]
ax2.plot(x, yr, '-r')
ax2.plot(x, yg, '-g')
ax2.plot(xb, yb, '-b')
ax2.hlines(turn_pt,0, 40, 'b', alpha=0.)
ax2.fill_between(xb, yb, 0, alpha=0.4)
ax2.set_ylim([0, 1.2])
ax2.set_xlim([0, 40])

There are a few solutions to get rid of the top x-axis and the right y-axis, please search older SO posts.

And finally, welcome to SO.

这篇关于创建Matplotlib图,使用Python中的子图通用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆