Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9077577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:24:04+00:00 2026-06-16T19:24:04+00:00

Suppose I have a text file with an arbitrary number of rows where each

  • 0

Suppose I have a text file with an arbitrary number of rows where each row gives some set of parameters that define a function (say the (x,y) location and sigmas (possibility unequal) of a 2D Gaussian). For example, in that case, the text file might contain:

100 112  3 4
97   38  8 9
88   79  3 9
    ...
    ...
102  152 9 5

I would like to plot (using pm3d) the SUM of all the distributions defined by the text file. How can that be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:24:05+00:00Added an answer on June 16, 2026 at 7:24 pm

    I would like to plot (using pm3d) the SUM of all the distributions defined by the text file. How can that be done?

    I think that cannot be done in native gnuplot, at least not in any sane way that I know. That kind of number crunching is not really what gnuplot is designed to do.

    Python, however, makes it pretty doable…

    #!/usr/bin/env python2
    
    import math
    import numpy
    import os
    
    class Gaussian(object):
        '''A 2D gaussian function (normalized), defined by
        mx: x mean position
        my: y mean position
        sx: sigma in x
        sy: sigma in y'''
    
        def __init__(self, mx, my, sx, sy):
            self.mx = float(mx)
            self.my = float(my)
            self.sx = float(sx)
            self.sy = float(sy)
    
        def value(self,x,y):
            '''Evaluates the value of a Gaussian at a certain point (x,y)'''
        prefactor = (1.0/(self.sx*self.sy*2*math.pi))
        ypart = math.exp(-(x - self.mx)**2/(2*self.sx**2))
        xpart = math.exp(-(y - self.my)**2/(2*self.sy**2))
        return prefactor*ypart*xpart
    
        def getLimits(self, devs):
            '''Finds the upper and lower x and y limits for the distribution,
            defined as points a certain number of deviations from the mean.'''
            xmin = self.mx - devs*self.sx
            xmax = self.mx + devs*self.sx
            ymin = self.my - devs*self.sy
            ymax = self.my + devs*self.sy
    
            return (xmin, xmax, ymin, ymax)
    
    def readGaussians(filename):
        '''reads in gaussian parameters from an input file in the format
        mx my sx sy
    
        This makes some assumptions about how perfect the input file will be'''
        gaussians = []
        with open(filename, 'r') as f:
            for line in f.readlines():
                (mx, my, sx, sy) = line.split()
                gaussians.append(Gaussian(mx, my, sx, sy))
    
        return gaussians
    
    def getPlotLimits(gaussians, devs):
        '''finds the x and y limits of the field of gaussian distributions.
        Sets the boundary a set number of deviations from the mean'''
        # get the limits from the first gaussian and work from there
        (xminlim, xmaxlim, yminlim, ymaxlim) = gaussians[0].getLimits(devs)
        for i in range(1, len(gaussians)):
            (xmin, xmax, ymin, ymax) = gaussians[i].getLimits(devs)
            if (xmin < xminlim):
                xminlim = xmin
            if (xmax > xmaxlim):
                xmaxlim = xmax
            if (ymin < yminlim):
                yminlim = ymin
            if (ymax > ymaxlim):
                ymaxlim = ymax
    
        return (xminlim, xmaxlim, yminlim, ymaxlim)
    
    def makeDataFile(gaussians, limits, res, outputFile):
        '''makes a data file of x,y coordinates to be plotted'''
        xres = res[0]
        yres = res[1]
    
        # make arrays of x and y values
        x = numpy.linspace(limits[0], limits[1], xres)
        y = numpy.linspace(limits[2], limits[3], yres)
        # initialize grid of z values
        z = numpy.zeros((xres, yres))
    
        # compute z value at each x, y point
        for i in range(len(x)):
            for j in range(len(y)):
                for gaussian in gaussians:
                    z[i][j] += gaussian.value(x[i], y[j])
    
        # write out the matrix
        with open(outputFile, 'w') as f:
            for i in range(len(x)):
                for j in range(len(y)):
                    f.write('%f %f %f\n' % (x[i], y[j], z[i][j]))
                f.write('\n')
    
    def makePlotFile(limits, outputFile):
        '''makes a plot file for gnuplot'''
        with open('plot.plt', 'w') as f:
            f.write("#!/usr/bin/env gnuplot\n")
            f.write("set terminal png font 'Courier'\n")
            f.write("set output 'gaussians.png'\n")
            f.write("set xr [%f:%f]\n" % (limits[0], limits[1]))
            f.write("set yr [%f:%f]\n" % (limits[2], limits[3]))
            f.write("set pm3d map\n")
            f.write("plot '%s' with image\n" % outputFile)
    
        # make plot file executable
        os.system('chmod 755 plot.plt')
    
        # plot
        os.system('./plot.plt')
    
    # name of input file
    inputFile = 'data.dat'
    # name of output (processed data)
    outputFile = 'gaussians.dat'
    # number of x and y points in plot
    res = (100, 100)
    # deviations from the mean by which to define Gaussian limits
    devs = 3
    
    # read in the gaussians from the data file
    print 'reading in data...'
    gaussians = readGaussians(inputFile)
    
    # find the plot limits
    limits = getPlotLimits(gaussians, devs)
    
    # make the gaussian data file
    print 'computing data for plotting...'
    makeDataFile(gaussians, limits, res, outputFile)
    
    # make the plot file
    print 'plotting...'
    makePlotFile(limits, outputFile)
    

    This script produces the following output when run on your example data.

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have 500 rows of data, each with a paragraph of text (like
Suppose I have a string that contains a text file, carriage returns and tabs
Suppose I have a text file that looks like this: 33 3 46 12
I run across this problem frequently suppose I have a text file that I
Suppose I have the following text in a text file First Text Some Text
Suppose I have a text file where each line contains either '1' or '-1.'
I have a set of values to written into a text file. Suppose I
Suppose I have a text file that says: This file has plain text Now,
Suppose I have a text file with some data I want to retrieve lost
Suppose I have a text that I can easily parse. It consists of text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.