Python code draws a love curve

Source: Internet
Author: User
Tags cos
Python's forte is to solve data analysis and visualization problems, this article is to share the Python code to draw a love curve of the operation of the detailed, the content is very good, I hope to help the needy friends.

Do not rush to draw the line of love, to see how to draw a function curve.

For example, draw a polynomial function for a specified interval:


The Python code is as follows:

Import NumPy as Npimport matplotlib.pyplot as PltX = Np.linspace ( -4, 4, 1024x768) Y =. * (x + 4.) * (x + 1.) * (X-2.) Plt.title (' $f (x) =\\frac{1}{4} (x+4) (x+1) (x-2) $ ') Plt.plot (x, Y, c = ' g ') plt.show ()

By NumPy the Linspace method to determine the range of the horizontal axis x, list the equation, and then call Matplotlib Pyplot to draw a function curve. NumPy is a scientific computing package implemented in Python, consisting of a powerful array of n-dimensional arrays and a mature library of functions, a toolkit for consolidating C + + and Fortran code, and a useful tool for linear algebra, Fourier transforms, and random number generation functions. Can be understood as MATLAB.

Remember Middle School, I asked the teacher trigonometric functions in the end what is the use? The teacher asked me, "If I give you a piece of tin, how can I cut the coal stove chimney?" Still remember the teacher's example, which seems to be abstract mathematical formulas, in fact, they do not know their application scenario only.


The Python code is as follows:

Import NumPy as Npimport matplotlib.pyplot as PltX = Np.linspace (0, 2 * np.pi, +) Ysinvalues = Np.sin (X) ycosvalues = np.c OS (x) plt.plot (x, Ysinvalues) plt.plot (x, Ycosvalues) plt.show ()

Matplotlib is a Python 2D drawing library that can even generate print quality-level graphics.

For those normal distributions, Python is quite simple to draw:

Import NumPy as Npimport Matplotlib.pyplot as Pltdef pdf (X, Mu, sigma):    a = 1./(Sigma * NP.SQRT (2. * Np.pi))    B = -1. /(2. * Sigma * * 2)    return a * NP.EXP (b * (X-MU) * * 2) X = Np.linspace ( -6, 6, +) for I in Range (3):    samples = n P.random.standard_normal    mu, sigma = Np.mean (samples), NP.STD (samples)    Plt.plot (x, PDF (x, Mu, sigma), color = '. $ ') Plt.plot (x, PDF (x, 0, 1.), color = ' B ') plt.show ()

In order not to appear monotonous, there are several curves drawn here. As long as the variance and the mean are calculated, what values are read from Excel to fit the normal distribution.



Return to the theme, about the love line, there is such a poignant love story.

Recently wandering in Wu Yue, a busy cloud empty and bright.

More than 300 years ago, on the streets of Stockholm, the wretched Descartes lived a begging life, all the belongings of shabby clothes and a few mathematical books with them. The lofty Descartes did not ask passers-by for charity, but silently bowed down on the paper to write and draw, concentrating on his mathematical world. A quiet afternoon, as usual, Descartes sat in the sunshine of the street to study mathematics problems, the crowd around the past, the noisy car and horse team, can not cause him interference.

There is a beautiful person, Wan as Qing. Encounter, and the son with Zang.

"What are you doing?" "Twisting, Descartes saw a young and beautiful eyelid pang, a pair of clear eyes such as the Blue Lake, lovely, she is Sweden's little princess, the king's favorite daughter Christine." She squatted down, took Descartes ' maths book and the grass paper, and talked to him. Speaking, he found that the little girl is quick-witted and has a strong interest in maths.

A few days later, Descartes accidentally received a notice, the king hired him to be a little princess math teacher. The puzzled Descartes came to the palace with the bodyguard, and he heard a bell-like laughter from afar. He saw the girl who met in the street that day, and from then on, he became the princess's maths teacher.

I don't know what's going on, it's a deep

The princess's maths progressed rapidly under Descartes ' careful guidance, and they began to become intimate. Descartes introduced her to the Cartesian coordinate system, in which algebra and geometry could be combined, the analytic geometry that Descartes created later. Under Descartes ' leadership, Kristen walked into the magical world of coordinates, and she was fascinated by the curve. Every day's inseparable also makes them have each other to have the affection heart.

In this romantic country of Sweden, a pure and beautiful love is sprouting.

Read to go, thousands of miles Lakeshore, Nightfall Chu days wide.

Their love spread to the king's ears, the King was furious, ordered the death of Descartes. At the begging of Christine, the king exiled him to his home, and the princess was put in house arrest.

At that time, the Black plague was prevalent in continental Europe. Shortly after Descartes returned to France, he contracted a serious illness. In the days of life into the countdown, he missed the day and night is the street met the warm smiling face. He insisted on writing to her every day and looked forward to hearing from her. However, the letters were intercepted by the king, and the princess had not received any information from him.

To know the heart, to read the book in the abdomen.

After Descartes sent the 13th letter to Kristen, he left the world forever. At this time, under house arrest in the palace of the little princess still wandering in the palace corridor, miss the distant lover.
This last letter does not say a word, there is only one equation: R=a (1-sinθ).
The king thought that there were two secrets hidden in the equation, and the whole city's mathematicians were summoned to the palace, but no one could solve the function. He could not bear to see his beloved daughter unhappy every day, and gave the letter to her. Christine was ecstatic to get the letter, she immediately understood the lover's intentions, to find paper and pens, drawing out the graphic, a heart-shaped pattern appeared in front of the eyes, Chris ting tears, this curve is the famous "heart-shaped line."

After the death of the king, Kristen inherited the throne, immediately sent to France to find the whereabouts of his sweetheart, received is the news of the death of Descartes, left a forever regret ... This world-renowned alternative love letter is said to have been kept in the European Descartes Memorial.

The source of this story is not known, the network spread a variety of versions, even in the Baidu Encyclopedia also has this story. Later, some people have verified the authenticity, think this is a beautiful lie, but does not hinder people to love line.

In a Cartesian coordinate system, the python representation of the equation of the love line is that x** 2+ y** 2 + a * x= a * sqrt(x** 2+y** 2) 和 x** 2+ y** 2 - a * x= a * sqrt(x** 2+y** 2) it is cumbersome to find the corresponding Y value by x, as in the case of a "universal layer" in software design, which can be represented by a parametric equation:

x=a* (2*cos (t)-cos (2*t)) y=a* (2*sin (t)-sin (2*T))

The specific Python code is as follows:

Import NumPy as Npimport matplotlib.pyplot as Plta = 1t = Np.linspace (0, 2 * np.pi, 1024x768) X = A * (2*np.cos (t)-np.cos (2*t)) Y = A * (2*np.sin (t)-np.sin (2*t)) Plt.plot (Y, x,color= ' R ') Plt.show ()

A heart-shaped line that represents love:



But this is not six lines of code AH? Nor is it r=a (1-sinθ)? That's true, that's polar coordinates, and Python's matplotlib also supports polar coordinates, and the six line Pyton code for the Love line is as follows:

Import NumPy as Npimport matplotlib.pyplot as Pltt = Np.linspace (0, 2 * np.pi, 1024x768) plt.axes (polar = True) Plt.plot (T, 1. -Np.sin (T), color= "R") Plt.show ()

In this way, the image in the cover is obtained:


Heart-shaped line is really love, if the integration of sadness will be what?

Import NumPy as Npimport matplotlib.pyplot as Pltx = Np.linspace ( -8, 8, 1024x768) y1 = 0.618*np.abs (x)-0.8* np.sqrt (64-x**2) y2 = 0.618*np.abs (x) + 0.8* np.sqrt (64-x**2) plt.plot (x, y1, color = ' R ') plt.plot (x, y2, color = ' R ') Plt.show ()

This gives you another line of love:


On the network there are all kinds of beautiful implementation of love line, but also full of various emotions, but for each, basically can use Python relatively concise implementation.


In fact, drawing is simple and difficult is the expression of the curve equations and the actual application requirements, such as the helix.




Further, you can draw a variety of 3-dimensional views, such as:





Summarize:

As the last link of data analysis and even large data processing, the visualization of the so-called insight, Python can be said to be one of the simple and practical tools.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.