Use python to draw a heart. Use python to draw a heart.

Source: Internet
Author: User
Tags install matplotlib

Use python to draw a heart. Use python to draw a heart.

Preface

Python generally uses Matplotlib to create statistical graphs. In its own statement, 'Make simple things simple and make complex thes '. You can use it to create line charts, histograms, bar charts, scatter plots, pie charts, and spectral charts, these images can be exported to multiple formats with publishing quality. In addition, it can be used in combination with ipython, which is really convenient. Who knows it! This article describes how to use matplotlib in python to draw a heart. If you are interested, let's take a look.

Install matplotlib

First install matplotlib

pip install matplotlib

Windows users can download and install the SDK on the official website. John Hunter (1968-2012), creator of matpltlib, just died on the official website. Thank you for creating such a powerful drawing tool.

Code on

#!/usr/bin/env python3from mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cmfrom matplotlib.ticker import LinearLocator, FormatStrFormatterimport matplotlib.pyplot as pltimport numpy as npdef heart_3d(x,y,z): return (x**2+(9/4)*y**2+z**2-1)**3-x**2*z**3-(9/80)*y**2*z**3def plot_implicit(fn, bbox=(-1.5, 1.5)): ''' create a plot of an implicit function fn ...implicit function (plot where fn==0) bbox ..the x,y,and z limits of plotted interval''' xmin, xmax, ymin, ymax, zmin, zmax = bbox*3 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') A = np.linspace(xmin, xmax, 100) # resolution of the contour B = np.linspace(xmin, xmax, 40) # number of slices A1, A2 = np.meshgrid(A, A) # grid on which the contour is plotted for z in B: # plot contours in the XY plane  X, Y = A1, A2  Z = fn(X, Y, z)  cset = ax.contour(X, Y, Z+z, [z], zdir='z', colors=('r',))  # [z] defines the only level to plot  # for this contour for this value of z for y in B: # plot contours in the XZ plane  X, Z = A1, A2  Y = fn(X, y, Z)  cset = ax.contour(X, Y+y, Z, [y], zdir='y', colors=('red',)) for x in B: # plot contours in the YZ plane  Y, Z = A1, A2  X = fn(x, Y, Z)  cset = ax.contour(X+x, Y, Z, [x], zdir='x',colors=('red',)) # must set plot limits because the contour will likely extend # way beyond the displayed level. Otherwise matplotlib extends the plot limits # to encompass all values in the contour. ax.set_zlim3d(zmin, zmax) ax.set_xlim3d(xmin, xmax) ax.set_ylim3d(ymin, ymax) plt.show()if __name__ == '__main__': plot_implicit(heart_3d) 

The effect is like this, which is interesting:

Summary

The above is all about this article. I hope this article will help you learn or use python. If you have any questions, please leave a message.

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.