Plotting Simple Functions

The following are a few examples of how to create a simple plot in Matlab/Octave, Python3, and Julia. I made a similar look between all three programs, trying to keep each as close to the defaults as possible, although all 3 are highly customizable. GNU Octave and Julia are both freely available, and Python interpreters are available for most operating systems. Matlab does cost money, but is nearly identical to GNU Octave (although Matlab has toolboxes and a nicer GUI).

The plot is simply the function y=x^2 with a domain of x=-10 to x=10. Each plot includes a grid, labeled x and y-axis, and title. The domain is computed with interval of 0.01 on all.

Octave/Matlab

x=-10:0.01:10;
y=x.^2;
plot(x,y)
grid on
xlabel('x')
ylabel('y')
title('Simple Plot y=x^2')
print simple_plot.png

Python

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
x = np.arange(-10.0, 10, 0.01)
y = x**2
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set(xlabel='x', ylabel='y', title='Simple Plot y=x^2')
ax.grid()
fig.savefig("simple_plot.png")
plt.show()

Affiliate link – I earn a commission if you shop through the link(s) below at no additional cost to you (more info)

Julia

using Plots
x=-10:0.01:10; y=x.^2;
plot(x,y,
    xlabel = "x",
    ylabel = "y",
    title = "Simple Plot y=x^2"
)
savefig("simpleplot.png")

Best tool for the job

It’s been a few years since I’ve done any programming at all, besides writing a few simple bash scripts. When I began looking out to see if there’s anything new to the table, I noticed that the Julia language has been rising in popularity. That’s why I’ve decided to include it here, I’m learning it as I brush up on the other two side-by-side.

I have quite an extensive background working with Matlab/Octave for both school and research. A lot of students in science or engineering are probably somewhat familiar with Matlab. It’s a good place to start since Matlab/Octave can both be used with an intuitive GUI which is helpful if you don’t have a strong programming/command line background.

While I’ve spent a bit of time programming with Python, I am far from proficient. Python took the most line of code to make a simple plot. Yet, I really like the matplotlib look out of the box.

After a few days of working with each, I can confidently say that I haven’t lost much when it comes to Octave, and Python is pretty intuitive. I’m still deciding how far I want to go with Julia (I felt weird writing that sentence). Setting it up on my Linux system has not been straight-forward. Getting it to run has been a pain. Although I like the language so far, it feels clean and straightforward. The default plot scheme also looks nice.

Support This Site

Consider becoming a member to access exclusive perks and support the sustainability of this valuable resource. Already a member? Log in here!

Level

Free

Trailblazer Monthly

Explorer Monthly

Patron Monthly

Price

Free.

$3.00 per Month.

$6.00 per Month.

$10.00 per Month.

Description

golden sunlight diffuses through the dense stand of snow-laden spruce trees, casting long shadows and bathing the forest in a warm, hazy light.

  Select Select Select Select
Occasional Newsletter Yes Yes Yes Yes
Free Articles Yes Yes Yes Yes
Exclusive Content* No Yes Yes Yes
Ad-free browsing** No No Yes Yes
Discounts on Prints No No Yes Yes
Priority Responses For Questions No No No Yes
Advanced Notice On New Content No No No Yes
More In-depth Content No No No Yes
  Select Select Select Select

Free

golden sunlight diffuses through the dense stand of snow-laden spruce trees, casting long shadows and bathing the forest in a warm, hazy light.

  • Occasional Newsletter
  • Free Articles
Select

Free.

Trailblazer Monthly

  • Occasional Newsletter
  • Free Articles
  • Exclusive Content*
Select

$3.00 per Month.

Explorer Monthly

  • Occasional Newsletter
  • Free Articles
  • Exclusive Content*
  • Ad-free browsing**
  • Discounts on Prints
Select

$6.00 per Month.

Patron Monthly

  • Occasional Newsletter
  • Free Articles
  • Exclusive Content*
  • Ad-free browsing**
  • Discounts on Prints
  • Priority Responses For Questions
  • Advanced Notice On New Content
  • More In-depth Content
Select

$10.00 per Month.

* I don’t plan on hiding much content behind a paywall because I believe it should be open and accessible to all. However, maintaining this website involves a significant investment of both time and money. I spend countless hours building and writing these pages and articles and incur thousands of dollars annually to keep the site running.

Your support through membership helps cover these expenses and allows me to continue providing high-quality content. Membership allows you to access exclusive perks and content and contribute to this valuable resource's sustainability. Thank you for your support!

** Ads and affiliate links will still be shown on relevant content, like in gear reviews.

   

Add a comment

*Please complete all fields correctly

Related Posts