Skip to content

Instantly share code, notes, and snippets.

@dieter-medium
Last active April 6, 2022 18:41
Show Gist options
  • Save dieter-medium/2271005cc5939e20c4f865fbbc2bf833 to your computer and use it in GitHub Desktop.
Save dieter-medium/2271005cc5939e20c4f865fbbc2bf833 to your computer and use it in GitHub Desktop.
require 'matplotlib/pyplot'
require 'matplotlib/axes_3d'
require 'numpy'
plt = Matplotlib::Pyplot
fig = plt.figure(num: 1, clear: true)
ax = fig.add_subplot(1, 1, 1, projection: '3d')
(theta, phi) = Numpy.meshgrid(Numpy.linspace(0, 2 * Numpy.pi, 41),
Numpy.linspace(0, 2 * Numpy.pi, 41))
x = (3 + Numpy.cos(phi)) * Numpy.cos(theta)
y = (3 + Numpy.cos(phi)) * Numpy.sin(theta)
z = Numpy.sin(phi)
def fun(t, f)
(Numpy.cos(f + 2 * t) + 1) / 2.0
end
ax.plot_surface(x, y, z, facecolors: Matplotlib.cm.jet(fun(theta, phi)))
ax.set(xlabel: 'x',
ylabel: 'y',
zlabel: 'z',
xlim: [-4, 4],
ylim: [-4, 4],
zlim: [-4, 4],
xticks: [-4, -2, 2, 4],
yticks: [-4, -2, 2, 4],
zticks: [-1, 0, 1],
title: 'Donut!')
fig.tight_layout()
plt.show()
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'matplotlib'
gem 'numpy'
require 'matplotlib/pyplot'
require 'matplotlib/axes_3d'
require 'numpy'
plt = Matplotlib::Pyplot
fig = plt.figure()
wf = fig.add_subplot(111, projection: '3d')
measured_results = []
(1..10).each do |with_in_chars|
measured_results.push []
(1..20).each do |cnt_words|
measured_results[-1].push with_in_chars + cnt_words + rand(5)
end
end
xr = (1..10).to_a
yr = (1..20).to_a
zi = []
xi, yi = Numpy.meshgrid(xr, yr)
(0...xi.to_a.size).each do |row|
zi.push []
(0...xi[row].size).each do |col|
x = xi[row][col] - 1
y = yi[row][col] - 1
zi[-1].push measured_results[x][y]
end
end
x = Numpy.asarray xi
y = Numpy.asarray yi
z = Numpy.asarray zi
wf.plot_wireframe(x, y, z, rstride: 2,
cstride: 2, color: 'green')
wf.set_title('Example')
plt.savefig('wireframe.svg')
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dieter-medium
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment