0

I have a python program that reads tsv data and plots it using the matplotlib library.

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. For this graphic, N=3 (3 reactions).

Gibbs free energy plots for 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.

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.

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?

1

1 Answer 1

1

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:

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.