Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在matplotlib中绘制子图的分界线_Python_Pandas_Numpy_Matplotlib_Graph - Fatal编程技术网

Python 在matplotlib中绘制子图的分界线

Python 在matplotlib中绘制子图的分界线,python,pandas,numpy,matplotlib,graph,Python,Pandas,Numpy,Matplotlib,Graph,我被要求生成一些用于多基线研究设计的图表,这是一种特殊类型的图表。我借此机会更多地了解了Matplotlib和Pandas,但有一件事我很难理解,那就是基地和干预之间的分界线。我需要它继续通过多个子地块,也可以很好地扩展。有没有办法完成这样一件事?我尝试过使用Lines.Line2D和ConnectionPatch进行实验,但我仍然无法正确地缩放和确定位置 到目前为止,我的(简单)代码 import matplotlib.pyplot as plt import numpy as np impo

我被要求生成一些用于多基线研究设计的图表,这是一种特殊类型的图表。我借此机会更多地了解了Matplotlib和Pandas,但有一件事我很难理解,那就是基地和干预之间的分界线。我需要它继续通过多个子地块,也可以很好地扩展。有没有办法完成这样一件事?我尝试过使用Lines.Line2D和ConnectionPatch进行实验,但我仍然无法正确地缩放和确定位置

到目前为止,我的(简单)代码

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

y = np.array([0,1,2,3,4])
fig, axs = plt.subplots(3, sharex=True, sharey=True)
fig.suptitle("I1 - Reakce na změnu prvku")
axs[0].plot(df.index,df['A'], color='lightblue', label="A")
axs[1].plot(df.index,df['N'], color='darkblue', label="N")
axs[2].plot(df.index,df['P'], color='blue', label="P")

plt.yticks(np.arange(y.min(), y.max(), 1))
plt.show()
到目前为止我的绘图(上述代码的结果):

上下文的示例图:


我的直觉是,这种问题是在图形坐标中画一条线。我遇到的一个问题是在连续轴之间找到中心区域的位置。我的代码很难看,但它可以工作,并且与每个轴的相对大小或轴之间的间距无关,如下所示:

从matplotlib.lines导入Line2D
def grouper(iterable,n,fillvalue=None):
“将数据收集到固定长度的块或块中”
#石斑鱼('ABCDEFG',3,'x')-->ABC DEF Gxx“
从itertools导入zip\u
args=[iter(iterable)]*n
返回zip_最长(*参数,fillvalue=fillvalue)
xconn=[0.15,0.35,0.7]#每个子批次中垂直线的位置,以数据坐标表示
图,axs=plt.子批次(3,1,gridspec_kw=dict(hspace=0.6,高度比=[2,0.5,1]))
#
#绘制分隔线,应在设定轴限值后的最末端绘制。
#
#将每个轴上的xconn值转换为地物坐标
xconn=[fig.transFigure.inversed().transform(ax.transData.transform([x,0]))[0]用于x,ax在zip中(xconn,axs)]
yconn=[]#y-连接线的值,以图形坐标表示
对于axs中的ax:
bbox=ax.get_位置()
yconn.extend([bbox.y1,bbox.y0])
#用平均值替换每对轴底部和顶部对应的每对值
yconn[1:-1]=np.ravel([[np.mean(ys)]*2表示石斑鱼中的ys(yconn[1:-1],2)])。tolist()
l=Line2D(np.repeat(xconn,2),yconn,transform=fig.transformure,ls='--',lw=1,c='k')
图添加艺术家(l)

借用,似乎使用
fig.transFigure
可以访问每个子地块中的坐标,并且您可以在所有这些坐标之间绘制线。这可能是最好的方法,因为它可以直接确定起点和终点。因为您的x坐标方便地从1-12开始,所以您也可以绘制每个子地块分为两部分的子图,在点之间留出间隙,以便注释线穿过

import numpy as np
import pandas as pd

import matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch

y = np.array([0,1,2,3,4])

## recreate your data
df = pd.DataFrame({
    'A':[0, 1, 1, 1, 2, 2, 3, 2, 3] + [float("nan")]*3,
    'N':[1, 0, 0, 2, 1, 1, 2, 3, 3, 3, 3, 3],
    'P':[0, 1, 1, 1, 2, 1, 1, 1, 2, 3, 3, 3],
    },  
    index=range(1,13)
)


fig, axs = plt.subplots(3, sharex=True, sharey=True)
fig.suptitle("I1 - Reakce na změnu prvku")

## create a gap in the line
axs[0].plot(df.index[0:3],df['A'][0:3], color='lightblue', label="A", marker='.')
axs[0].plot(df.index[3:12],df['A'][3:12], color='lightblue', label="A", marker='.')

## create a gap in the line
axs[1].plot(df.index[0:8],df['N'][0:8], color='darkblue', label="N", marker='.')
axs[1].plot(df.index[8:12],df['N'][8:12], color='darkblue', label="N", marker='.')

## create a gap in the line
axs[2].plot(df.index[0:10],df['P'][0:10], color='blue', label="P", marker='.')
axs[2].plot(df.index[10:12],df['P'][10:12], color='blue', label="P", marker='.')

plt.yticks(np.arange(y.min(), y.max(), 1))


transFigure = fig.transFigure.inverted()

## Since your subplots have a ymax value of 3, setting the end y-coordinate
## of each line to just above that value should help it display outside of the figure

coord1 = transFigure.transform(axs[0].transData.transform([3.5,3]))
coord2 = transFigure.transform(axs[1].transData.transform([3.5,3.5]))
coord3 = transFigure.transform(axs[1].transData.transform([8.5,3.5]))
coord4 = transFigure.transform(axs[2].transData.transform([8.5,3.5]))
coord5 = transFigure.transform(axs[2].transData.transform([10.5,3.5]))
coord6 = transFigure.transform(axs[2].transData.transform([10.5,0]))

## add a vertical dashed line
line1 = matplotlib.lines.Line2D((coord1[0],coord2[0]),(coord1[1],coord2[1]),
                               transform=fig.transFigure,
                               ls='--',
                               color='grey')

## add a horizontal dashed line
line2 = matplotlib.lines.Line2D((coord2[0],coord3[0]),(coord2[1],coord3[1]),
                               transform=fig.transFigure,
                               ls='--',
                               color='grey')

## add a vertical dashed line
line3 = matplotlib.lines.Line2D((coord3[0],coord4[0]),(coord3[1],coord4[1]),
                               transform=fig.transFigure,
                               ls='--',
                               color='grey')

## add a horizontal dashed line
line4 = matplotlib.lines.Line2D((coord4[0],coord5[0]),(coord4[1],coord5[1]),
                               transform=fig.transFigure,
                               ls='--',
                               color='grey')

## add a vertical dashed line
line5 = matplotlib.lines.Line2D((coord5[0],coord6[0]),(coord5[1],coord6[1]),
                               transform=fig.transFigure,
                               ls='--',
                               color='grey')

fig.lines.extend([line1, line2, line3, line4, line5])
plt.show()

听起来您想在图形中添加注释,如垂直或水平虚线,以及文本?
matplotlib
有这样的注释,我很乐意为您指出正确的方向或给您一个示例是的,但更具体地说,我需要“外部”示例图中的虚线“子地块的范围,并创建无缝线路。我可以在每个子图上画垂直线,这也会起作用,但我很好奇有没有一种方法可以像示例中那样画出来。哇,非常感谢!这正是我想要的。你的评论也写得很好,解释性很强,而不是“woodoo magic”的代码块。没问题——很高兴我的回答很有帮助!