I am trying to export an image generated in a Jupyter notebook using the H2O library to a PNG file. The image is the variable importance plot
I have tried using the matplotlib export functionality but it generates an empty PNG file.
cv_gbm.varimp_plot()
I don't know the direct solution to your question, but the following steps should work:
gbm_var_imp = cv_gbm._model_json['output']['variable_importances'].as_data_frame()
x = gbm_var_imp['scaled_importance']
x.index = gbm_var_imp['variable']
fig = plt.figure(figsize=(8, 8))
x.sort_values().plot(kind='barh')
fig.savefig('gbm_variable_importance.png', dpi=600)
Related
I just start the module worcloud in Python 3.7, and I'm using the next cxode to generate wordclouds from a dictionary and I'm trying to use differents masks, but this works for some images: in two cases works with images of 831x816 and 1000x808. This has to be with the size of the image? Or is because the images is kind a blurry? Or what is it?
I paste my code:
from PIL import Image
our_mask = np.array(Image.open('twitter.png'))
twitter_cloud = WordCloud(background_color = 'white', mask = our_mask)
twitter_cloud.generate_from_frequencies(frequencies)
twitter_cloud.to_file("twitter_cloud.jpg")
plt.imshow(twitter_cloud)
plt.axis('off')
plt.show()
How can i fix this?
I had a similar problem with a black-and-white image I used. What fixed it for me was when I cropped the image more closely to the black drawing so there was no unnecessary bulk white area on the edges.
Some images should be adjusted for the process. Note only white point values for image is mask_out (other values are mask_in). The problem is that some of images are not suitable for masking. The reason is that the color's np.array somewhat mismatches. To solve this, following can be done:
1.Creating mask object: (Please try with your own image as I couldn't upload:)
import numpy as np;
import pandas as pd;
from PIL import Image;
from wordcloud import WordCloud
mask = np.array(Image.open("filepath/picture.png"))
print(mask)
If the output values for white np.array is 255, then it is okay. But if it is 0 or probably other value, we have to change this to 255.
2.In the case of other values, the code for changing the values:
2-1. Create function for transforming (here our value = 0)
def transform_zeros(val):
if val == 0:
return 255
else:
return val
2-2. Creating the same shaped np.array:
maskable_image = np.ndarray((mask.shape[0],mask.shape[1]), np.int32)
2-3. Transformation:
for i in range(len(mask)):
maskable_image[i] = list(map(transform_zeros, mask[i]))
3.Checking:
print(maskable_image)
Then you can use this array for your mask.
mask = maskable_image
All this is copied and interpreted from this link, so check it if you find my attempted explanation unclear, as I just provided solution but don't understand that much about color arrays of image and its transformation.
I am trying to plot an image after some processing. I get three different images using the three options below. The image obtained is after applying the Sobel filter twice on a road lane image.
sample_image.jpg
The three methods to plot are shown in the below Python code.
img = cv2.imread('sample_image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gaussian = cv2.GaussianBlur(gray,(3,3),0)
sobely = cv2.Sobel(gaussian,cv2.CV_64F,1,0,ksize=5) # y
sobelyy = cv2.Sobel(sobely,cv2.CV_64F,1,0,ksize=5) # y
# method 1
cv2.imshow('sobelyy', sobelyy)
# method 2
cv2.imwrite('filtered_img1.JPG', sobelyy)
s_img = cv2.imread('filtered_img1.JPG')
cv2.imshow('s_img', s_img)
# method 3
plt.figure()
plt.imshow(sobelyy, cmap='gray')
plt.title('Filtered sobelyy image, B(x,y)'), plt.xticks([]), plt.yticks([])
plt.show()
The images I get are:
method 1
method 2
method 3
The image I want to get is the one obtained in method 3.
Why are the images shown in different ways?
How can I get to save the output image like the result of method 3?
Thank you in advance!
Why are the images shown in different ways?
OpenCV and Matplotlib use different color spaces to display images - that's why they look differently even when they are actually the same.
As for your first 2 methods those should actually look the same and they do when I try out your code.
How can I get to save the output image like the result of method 3?
Matplotlib has a build in function to write plotted images to disc, just use:
plt.savefig('your_filename.png')
I am currently working on a program that requires me to read DICOM files and display them correctly. After extracting the pixel array from the DICOM file, I ran it through both the imshow function from matplotlib and cv2. To my surprise they both yield vastly different images. One has color while the other has no, and one shows more detail than the other. Im confused as to why this is happening. I found Difference between plt.show and cv2.imshow? and tried converting the pixels to BRG instead of RGB what cv2 uses but this changes nothing. I am wondering why it is that these 2 frameworks show the same pixel buffer so differently. below is my code and an image to show the outcomes
import cv2
import os
import pydicom
import numpy as np
import matplotlib.pyplot as plt
inputdir = 'datasets/dicom/98890234/20030505/CT/CT2/'
outdir = 'datasets/dicom/pngs/'
test_list = [ f for f in os.listdir(inputdir)]
for f in test_list[:1]: # remove "[:10]" to convert all images
ds = pydicom.dcmread(inputdir + f)
img = np.array(ds.pixel_array, dtype = np.uint8) # get image array
rows,cols = img.shape
cannyImg = cv2.Canny(img, cols, rows)
cv2.imshow('thing',cv2.cvtColor(img, cv2.COLOR_BRG2RBG))
cv2.imshow('thingCanny', cannyImg)
plt.imshow(ds.pixel_array)
plt.show()
cv2.waitKey()
Using the cmap parameter with imshow() might solve the issue. Try this:
plt.imshow(arr, cmap='gray', vmin=0, vmax=255)
Refer to the docs for more info.
Not an answer but too long for a comment. I think the root cause of your problems is in the initialization of the array already:
img = np.array(ds.pixel_array, dtype = np.uint8)
uint8 is presumably not what you have in the DICOM file. First because it looks like a CT image which is usually stored with 10+ bpp and second because the artifacts you are facing look very familiar to me. These kind of artifacts (dense bones displayed in black, gradient effects) usually occur if >8 bit pixeldata is interpreted as 8bit.
BTW: To me, both renderings look obviously incorrect.
Sorry for not being a python expert and just being able to tell what is wrong but unable to tell how to get it right.
I developed a python script that plots data from netcdf files. The problem is that I try to modify the title of the plot ( and in other cases I also tried to change x axis title with ax.set_xlabel('etc') ) but python seems not answering.
The data are extracted using xarray and then are plotted using matplotlib.
This is my code:
from os.path import expanduser
home = expanduser("~") # Get users home directory
file = home + '/Desktop/Tesi/Triennial_EOFs/' # Adjust if necessary
path = file + 'normevectortime1905_1.nc'
mask = xr.open_dataset(path, decode_times=False) #extract variables in xarray from .nc file
in_variable='msl'
var = mask[in_variable] #variable to study
ax = plt.subplot(1,1,1, projection=proj)
ax.set_title('PLOT')
ax.set_xlabel('x axes_test')
ax.coastlines(resolution='50m',linewidth=0.4)
var[0,:,:].plot.contourf(transform=crs.PlateCarree(),levels=20)
plt.show()
This is my output
Moreover I never explicitly written code lines for the colorbar or the text related to the variable field near the colorbar. And I never even wrote instructions for written time on the top of the plot.
Okay, so I am trying to create some animation in matplotlib. I am doing this on Jupyter-Notebook.
I am converting the animation using to_html5_video(). And displaying it using HTML(). The problem is that this video is not fitting in my cell.
fig, ax = plt.subplots()
l, = ax.plot([],[], "k.")
ax.set_xlim([0,L])
ax.set_ylim([0,L])
def animate(i):
l.set_data(xPos[:i], yPos[:i])
ani = animation.FuncAnimation(fig, animate,frames=len(xPos)).to_html5_video()
HTML(ani)
It is looking like this:
How do I fit it properly?
When I had this problem I found that I could make the video smaller by using plt.rcParams["savefig.dpi"] = 100. This gave me the notion that my config was somehow bad, so I simply deleted ~/.matplotlib/matplotlibrc and the sample code for animations now render correctly.