How to Plot Math Expressions in Python + R
You might have thought that plotting functions was something that could be left behind in high school. Now when teenagers ask you "when would I ever need to plot functions" you can answer something like, "So we can create simulations of the real world!" Being able to simulate data can be very useful for data scientists. In order to simulate data we can start with a simple mathematical expression. In this example we will use a quadratic but it could be any expression you can like.
The goal over the next few weeks will be to simulate data from a known mathematical expression and then fit a model to that data and see how well we can fit the data. Before we get to the simulation part we will create functions to plot our known expressions. Next week we will continue with the simulation part. We are creating plotting functions so that we can use them later with our simulated data as well.
In Python we can get the job done with Matplotlib and Numpy.
In R we will need ggplot2 and I'm using the latex2expression package to render the mathematical formula nicely.
So you see it's quite possible to plot mathematical functions. You can substitute almost any regular function for x into the plotting functions and see what they look like. This is step one in the 4 step process of simulating data for data science. Next week will use derivatives of these functions to visualise simulated data based on our know math expressions.
--
2 年Thanks for posting