[PYTHON] Create a graph using the Sympy module

Create a graph with Sympy

When doing algebraic calculations using Sympy, you may want to take a quick look at what the graph of this formula looks like. Speaking of graphs in Python, I think the first thing that comes to mind is matplotlib, but when $ y = f (x) $, $ x $ needs to be assigned using numpy's arrange, which is troublesome. .. Sympy's Plotting module can be used in such cases. This not only graphs the formula as it is, but also does something quite complicated.

Import required modules

First, import the required modules. In Sympy, it seems that "from sympy import ~" is often used. In addition to the var that defines the variables, this time I will use the "plot_implicit function" that allows you to experience the greatness of the Plotting module. Also, in fact, the graph of Sympy has matplotlib running behind the scenes, so import this as well.

Import necessary modules etc.


from sympy import var
from sympy.plotting import plot_implicit
import matplotlib.pyplot as plt

Creating a circle

First, let's draw a circle ($ x ^ 2 + y ^ 2 = 1 $).

Import necessary modules etc.


#Drawing an ellipse
var('x,y')
size = 1.1
f = x**2 + y**2 - 1
plot_implicit(f, (x, -size, size), (y, -size, size),
              xlim=(-size*1.1, size*1.1), ylim=(-size*1.1, size*1.1))
plt.rcParams['figure.figsize'] = (5, 5)

With plot_implicit, you can graph the formula nicely without having to make it look like $ y = f (x) $. You can create a graph just by entering a formula and specifying the range of $ x, y $ with plot_implicit. After that, in plt.rcParams of matplotloib, specify the size of the graph etc. and it is completed. The result is as follows: image.png

Drawing a hyperbola

Next, let's draw a hyperbola. The formula is $ x ^ 2- y ^ 2 = 1 $. The program and graph are as follows.

Import necessary modules etc.


#Drawing a hyperbola
var('x,y')
size = 5
f= x**2 - y**2 - 1
plot_implicit(f, (x, -size, size), (y, -size, size),
              xlim=(-size*1.1, size*1.1), ylim=(-size*1.1, size*1.1))
plt.rcParams['figure.figsize'] = (5, 5)

image.png

Two straight lines that intersect

Let's draw the last two straight lines that intersect. The formula is $ f = x ^ 2 --y ^ 2 --0 $. The program and graph are as follows.

Import necessary modules etc.


 #Drawing intersecting curves
var('x,y')
size = 5
f= x**2 - y**2 - 0
plot_implicit(f, (x, -size, size), (y, -size, size),
              xlim=(-size*1.1, size*1.1), ylim=(-size*1.1, size*1.1))
plt.rcParams['figure.figsize'] = (5, 5)

image.png

Summary

Suddenly I used a special function, but I think it's very convenient. However, it seems that plot_implicit cannot be used in 3D, so you need to use another module. I'm doing a little more research on this.

Recommended Posts

Create a graph using the Sympy module
Create a Python module
Write a TCP server using the SocketServer module
Create a GUI on the terminal using curses
Create a record with attachments in KINTONE using the Python requests module
Creating a graph using the plotly button and slider
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
Create a dictionary by searching the table using sqlalchemy
Create a real-time auto-reply bot using the Twitter Streaming API
Create a nested dictionary using defaultdict
Try using the Python Cmd module
Create a CRUD API using FastAPI
Create a C wrapper using Boost.Python
Create a pandas Dataflame by searching the DB table using sqlalchemy
[Ev3dev] Create a program that captures the LCD (screen) using python
Create a compatibility judgment program with the random module of python.
Create a graph that displays an image with a mouse hover using the data visualization library Dash
How to draw a graph using Matplotlib
[Python] Create a Batch environment using AWS-CDK
Create an application using the Spotify API
Create a dataframe from excel using pandas
Add a layer using the Keras backend
Create a graph with borders removed with matplotlib
Create a GIF file using Pillow in Python
[pyqtgraph] Understand SignalProxy and create a crosshair that follows the cursor on the graph
Create a beauty pageant support app using PyLearn2
[Python] Let's execute the module regularly using schedule
Create a REST API using the model learned in Lobe and TensorFlow Serving.
Create the strongest calculator environment with Sympy + Jupyter
Easy to create API server using go-json-rest module
Let's create a REST API using SpringBoot + MongoDB
Create a phylogenetic tree from Biopyton using ClustalW2
Create a binary data parser using Kaitai Struct
Save an array of numpy to a wav file using the wave module
Create a web map using Python and GDAL
I tried using the Datetime module by Python
[Python] A rough understanding of the logging module
Use the e-paper module as a to-do list
Try using the collections module (ChainMap) of python3
Create a visitor notification system using Raspberry Pi
Generate a hash value using the HMAC method.
Create a Mac app using py2app and Python3! !!
[AWS Lambda] Create a deployment package using the Docker image of Amazon Linux
Create a MIDI file in Python using pretty_midi
Let's make a module for Python using SWIG
Create a wheel of your own OpenCV module
How to uninstall a module installed using setup.py
Create a command to get the work log
Create a translation tool with the Translate Toolkit
Create a filter to get an Access Token in the Graph API (Flask)
A story that was convenient when I tried using the python ip address module
[Python] You can save an object to a file by using the pickle module.
Draw a graph with PyQtGraph Part 5-Increase the Y-axis
Read the Python-Markdown source: How to create a parser
Create a data collection bot in Python using Selenium
Create a function to visualize / evaluate the clustering result
Get the file name in a folder using glob
Cut a part of the string using a Python slice