Been experimenting with using Ruby inside a jupyter notebook. With Python I can do this
import matplotlib.image as mpimg
Does anyone know the equivalent with Ruby, I have not been able to find it in any of the iRuby or sciruby documentation?
To clarify a little, In my gemfile I have this
gem 'iruby'
gem 'cztop'
gem 'matplotlib'
But cannot seem to get access to the image part of matplotlib that I am use to using in python.
I am trying to find the Ruby version of what, in Python, I would write like this
#importing some useful packages
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
...
%matplotlib inline
#reading in an image
image = mpimg.imread('test_images/solidWhiteRight.jpg')
#printing out some stats and plotting
print('This image is:', type(image), 'with dimesions:', image.shape)
plt.imshow(image) #call as plt.imshow(gray, cmap='gray') to show a grayscaled image
Thanks so much for any suggestions
This is how far I can get it to work in jupyter notebook on MacOSX:
require 'matplotlib/pyplot'
plt = Matplotlib::Pyplot
image = plt.imread 'test.png'
puts "This image's dimensions: #{image.shape}"
plt.imshow(image)
plt.show()
I used your Gemfile with the additional gem rbczmq to avoid the kernel dying (hint found here):
gem 'iruby'
gem 'cztop'
gem 'matplotlib'
gem 'rbczmq'
Note that I used a .png because matplotlib can only read PNGs natively without PIL installed.
This is how the result will look like:
Displaying the result inline as in the python version:
seems to be impossible.
Related
Following the floWeaver tutorial and beginning with the Quickstart where flows are defined, the second step of plotting the flows does not work. The problem is that I do not get any error message.
The code consists of two blocks, with the second block not working..
import pandas as pd
flows = pd.read_csv('simple_fruit_sales.csv')
from ipysankeywidget import SankeyWidget
SankeyWidget(links=flows.to_dict('records'))
https://sankeyview.readthedocs.io/en/latest/tutorials/quickstart.html
Python version installed: 3.6.10
floWeaver version: 2.0.0
Many thanks in advance!
Just make sure that you have the following installation, other than the package itself.
pip install ipysankeywidget
jupyter nbextension enable --py --sys-prefix ipysankeywidget
jupyter nbextension enable --py --sys-prefix widgetsnbextension
With seaborn 0.11.1 and matplotlib 3.3.4 this...
import matplotlib as mpl
from matplotlib import pyplot as plt
print(sns.__version__, mpl.__version__)
import seaborn as sns
sns.set_theme(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)
plt.show()
...the result is supposed to look like this:
However, when I do that in JupyterLab or Plotly-Dash I get:
I deleted ~/.config/matplotlib and setup a clean conda environment with minimal packages. Same result.
plt.rcParams['figure.autolayout'] = False
fixes it.
I am trying to capture an image from my web camera on windows using Python 3.
I checked already openCV, but the support of python-3 is missing.
Is there any other way to do so?
In the meantime, OpenCV 3.1 was released and works with Python 3 (since OpenCV 3.0). Pre-compiled Windows binaries can be found here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv
you can try OpenCV, SimpleCV.
using SimpleCV:
from SimpleCV import Image, Camera
cam = Camera()
img = cam.getImage()
img.save("filename.jpg")
using OpenCV:
from cv2 import *
# initialize the camera
cam = VideoCapture(0) # 0 -> index of camera
s, img = cam.read()
if s: # frame captured without any errors
namedWindow("cam-test",CV_WINDOW_AUTOSIZE)
imshow("cam-test",img)
waitKey(0)
destroyWindow("cam-test")
imwrite("filename.jpg",img) #save image
using pygame:
import pygame
import pygame.camera
pygame.camera.init()
pygame.camera.list_camera() #Camera detected or not
cam = pygame.camera.Camera("/dev/video0",(640,480))
cam.start()
img = cam.get_image()
pygame.image.save(img,"filename.jpg")
Install OpenCV:
install python-opencv bindings, numpy
Install SimpleCV:
install python-opencv, pygame, numpy, scipy, simplecv
get latest version of SimpleCV
Install pygame:
install pygame
I've been animating evolution of surfaces (stored as 3d numpy arrays) using matplotlib, but this method is slow and clunky. I gather that VPython and MayaVi are better alternatives, but apparently neither is available yet in Python 3.4 (which is what I'm using). Are there any good ways to perform 3d animation in Python 3.4?
You can get the vpython working in python3.4 now. Follow this steps:
Preparation
Download the four packages TTFQuery, FontTools, and Polygon ,vpython in get the requisites packages to same directory.
Installation
Run cmd
cd path/to/package_downloaded_directory
pip install packagename.whl
Find the file C:\Python34\lib\site-packages\vis\materials.py
Open the file in an editor, then go to line 70
Comment the two lines just like this
class raw_texture(cvisual.texture):
def __init__(self, **kwargs):
cvisual.texture.__init__(self)
# for key, value in kwargs.items():
# self.__setattr__(key, value)
Save the changes
Note that when when you use vpython to code a script, the first line must be like below:
from vis import *
Below is my code sample
from vis import *
sphere(pos=vector(0,0,0),radius=0.5,color=color.red)
arrow(pos=vector(0.5,0,0),axis=vector(1,0,0),color=color.green)
While trying to install matplotlib, I've received the following error message (see bottom):
C:\Users\UserName>easy_install matplotlib
Searching for matplotlib
Reading http://pypi.python.org/simple/matplotlib/
Reading http://matplotlib.sourceforge.net
Reading https://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.0
Reading https://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-0.99.3/
Reading http://sourceforge.net/project/showfiles.php?group_id=80706&package_id=82474
Reading https://sourceforge.net/project/showfiles.php?group_id=80706&package_id=278194
Reading http://sourceforge.net/project/showfiles.php?group_id=80706
Reading https://sourceforge.net/project/showfiles.php?group_id=80706&package_id=82474
Reading https://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-0.99.1/
Reading https://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.0.1/
Best match: matplotlib 1.0.1-r0
Downloading http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.0.1/matplotlib-1.0.1_r0-py2.6-win32.egg/download
Processing download
error: Couldn't find a setup script in c:\users\username\appdata\local\temp\easy_install-xrixnr\download
Environment: Python 2.6, Win7 64bit
Any idea what's missing?
As explained in the instructions:
Install numpy - you can use easy_install numpy
Install matplotlib from an installation binary
You're also invited to install ipython, although it is not mandatory - you can use easy_install ipyhton