简体   繁体   English

Matplotlib - 颜色编码数据绘图线?

[英]Matplotlib - color-coding data plot lines?

I have a python program that reads tsv data and plots it using the matplotlib library.我有一个 python 程序,它读取 tsv 数据并使用 matplotlib 库绘制它。

I feel like my code works pretty well:我觉得我的代码运行良好:

def main(compsPath: str, gibbsPath: str):
    """
    Given the file paths for comps.tsv and
    gibbs.tsv, this main function will
    produce two separate plots - one for each file.
    """

    # Read tsv data into np record arrays
    # Slice off header text

    with open(compsPath, 'r') as fcomps:
        reader = csv.reader(fcomps, delimiter='\t')
        compsHeader = next(reader)
        compsData = np.array(list(reader)).astype(np.double)

    with open(gibbsPath, 'r') as fgibbs:
        reader = csv.reader(fgibbs, delimiter='\t')
        gibbsHeader = next(reader)
        gibbsData = np.array(list(reader)).astype(np.double)

    # Get data dimensions:
    # - - - M := Number of metabolites
    # - - - N := Number of reactions

    M = compsData.shape[1] - 1
    N = gibbsData.shape[1] - 1

    plotComps(M, compsData, compsHeader)
    plotGibbs(N, gibbsData, gibbsHeader)

    plt.show()

The plotGibbs function produces the following graphic for the tsv file I'm working with. plotGibbs 函数为我正在使用的 tsv 文件生成以下图形。 For this graphic, N=3 (3 reactions).对于此图形,N=3(3 个反应)。

N = 3 时的吉布斯自由能图。

I would like to indicate at what point in time each reaction becomes unfavorable (in the context of my project, this just means that the reaction stops).我想指出每个反应在什么时候变得不利(在我的项目中,这只是意味着反应停止)。 This occurs when the gibbs free energy value (∆G) of the reaction is greater than or equal to 0.当反应的吉布斯自由能值 (ΔG) 大于或等于 0 时,就会发生这种情况。

I feel like I could best emphasize this by color-coding the line plots my program generates.我觉得我可以通过对程序生成的线图进行颜色编码来最好地强调这一点。 For negative ∆G values, I would like the line to be green, and for positive or zero ∆G values, I would like the line to be red.对于负 ∆G 值,我希望线条为绿色,对于正或零 ∆G 值,我希望线条为红色。

Here is my current code for generating the gibbs free energy plots (does not color-code):这是我当前生成吉布斯自由能图的代码(没有颜色代码):

def plotGibbs(N: int, gibbsData: np.ndarray, gibbsHeader):

    gibbsFig = plt.figure()
    gibbsFig.suptitle("∆G˚ Yield Plotted over Time (days)")

    numCols = ceil(N / 2)
    numRows = (N // numCols) + 1

    for n in range (1, N+1):
        ax = gibbsFig.add_subplot(numRows, numCols, n)
        ax.set_ylabel(gibbsHeader[n])
        ax.set_xlabel(gibbsHeader[0])
        ax.plot(gibbsData[:, 0], gibbsData[:, n])

    gibbsFig.tight_layout()

How could I make it so that negative values are plotted green, and non-negative values are plotted red?我怎样才能使负值绘制为绿色,而非负值绘制为红色?

You could try to find where a change of sign occurs in your data using np.where with a simple condition like gibbsData[:, n]>0 then plot negative/positive data accordingly:您可以尝试使用np.where和一个简单的条件(如gibbsData[:, n]>0数据中发生符号变化的位置,然后相应地绘制负/正数据:

def plotGibbs(N: int, gibbsData: np.ndarray, gibbsHeader):

    gibbsFig = plt.figure()
    gibbsFig.suptitle("∆G˚ Yield Plotted over Time (days)")

    numCols = ceil(N / 2)
    numRows = (N // numCols) + 1

    for n in range (1, N+1):
        ax = gibbsFig.add_subplot(numRows, numCols, n)
        ax.set_ylabel(gibbsHeader[n])
        ax.set_xlabel(gibbsHeader[0])
        # idx where sign change occurs for data n
        idx_zero = np.where(gibbsData[:, n]>0)[0][0]
        # negatives y values
        ax.plot(gibbsData[:idx_zero, 0], gibbsData[:idx_zero,n],'g') 
        # positive y values
        ax.plot(gibbsData[idx_zero:, 0], gibbsData[idx_zero:,n],'r') 

    gibbsFig.tight_layout()

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

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