Last active
April 6, 2022 18:41
-
-
Save dieter-medium/2271005cc5939e20c4f865fbbc2bf833 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
source "https://rubygems.org" | |
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } | |
gem 'matplotlib' | |
gem 'numpy' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Explanations: https://medium.com/@dieter.s/visualizations-with-ruby-cdc16c0cee2e