English 中文(简体)
Plotting by Using gnuplot and Glade
原标题:

I am a new programmer. It is not my job but i want to learn python. I am a Windows 10 user and I am using the folllowing programs: Python 3.8 Gtk+ Glade Gnuplot Pycharm. I have installed the first 4 programs by using msys2 and Pycharm as standalone.

I want to add gnuplot chart to the gui by using glade. Also i couldn t show the gnuplot to the Pycharm. It does not know the "import Gnuplot" command. Can anyone help me?

问题回答

I think you need to import the module using this instead:

import PyGnuplot

Or possibly

import gnuplot-py

I see examples both ways.

Welcome to programming! It s always great to see new people learning Python.

The error message you re seeing might be due to PyCharm not recognizing the Gnuplot package. It seems you are trying to import Gnuplot with the command import Gnuplot. Instead, you should use the command import PyGnuplot.

Let s consider a simple example of how to use PyGnuplot for plotting and fitting a function. This might help you better understand the library and its syntax:

First, we need to import the necessary libraries:

from PyGnuplot import gp
import numpy as np

Next, we create a new figure instance:

fig1 = gp()
fig1.default_term =  wxt 

Now, let s generate some data to plot:

x = np.linspace(0, 20, 1001)
yn = np.random.randn(1001)/10
y = np.sin(x)
data = [x, y+yn]

In this case, we re generating x as a sequence of evenly spaced numbers from 0 to 20. The y values are a noisy sine wave. The yn variable is used to add some random noise to the y values to make the data look more like real-world data.

Next, let s define a function to fit to our data:

func =  y(x) = a + b*cos(x + c)   

This is a simple cosine function that we ll try to fit to our sine wave data. The a, b, and c are parameters of the function that will be adjusted to best fit the data.

Now, let s fit our function to the data:

(a, b, c), report = fig1.fit2d(data, func, via= a,b,c , limit=1e-9)

The fit2d function takes our data and function, and adjusts the parameters a, b, and c to minimize the difference between the function and the data. The limit argument specifies the stopping criterion for the fitting process.

Finally, let s plot the data and the fitted function:

fig1.save(data, "tmp.dat")
fig1.a( plot "tmp.dat" w lp )
fig1.a( replot y(x) )

This will first save the data to a file called "tmp.dat", then plot this data using points (lp stands for "linespoints"). The last line replots the fitted function on top of the data.

I hope this helps you get started with PyGnuplot! Please let me know if you have any further questions.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签