2016-06-15 68 views
1

我想比較使用Python(使用matplotlib)獲得的條形圖和Matlab函數的繪圖。爲此,我考慮將Matlab圖形疊加在條形圖的圖像上。將Matlab繪圖疊加在python繪圖/圖像上

但是我對matlab(python也很新奇),所以我不知道該怎麼做。我一直在關注這個話題的幾個問題,但是我真的不明白我應該怎麼做。

Bargraph from Python

A1 = 80; 
A2 = 72; 
B1 = 470; 
B2 = 220; 
D1 = 500; 
D2 = 700; 
T = 0.136994238205; 
X = -60:0.1:60; 
Y1 = A1*sqrt(D1*pi)*exp(-(X.^2)/(4*D1*T)) + B1*exp(-(X.^2)/(4*D1*T)); 

img = imread('bar2.png'); 

imagesc(X,Y1,flipud(img)); 
hold on; 
plot(X,Y1,'r','linewidth',1.5); 
set(gca, 'ydir', 'normal'); 

這是我已經嘗試了代碼,但顯然它不工作,我剛纔一直在試圖複製和了解其他問題,這裏給出的代碼。如何在條形圖上疊加此功能plot(X,Y1)?我知道我應該展示更多的編碼工作,但是我不知道如何解決這個問題,而且我也找不到任何有用的東西。我如何疊加一般?

有沒有一種方法來調整它,因爲它好像我應該翻譯它。 這就是我得到的 Plot over Bar Graph

回答

1

如果你有兩個圖(說.png文件),並希望疊加圖像,然後使用blend()。以下代碼將所有圖像疊加在「images2 /」文件夾中,併爲您提供單個最終圖像。

如果混合()更多圖像,則圖像的質量(顏色,亮度,對比度)可能會變差。使用ImageEnhance可以提高圖像的質量。 alpha的值可以調整以獲得完美的混合。

import Image 
import os 
import cv2 
import ImageEnhance 
listing = os.listdir("images2/") 
new_img = Image.new("RGBA", (800, 600), "white") 

for file in listing: 
    im = Image.open(path1 + file) 
    new_img = Image.blend(im, new_img, 2.0) 
    new_img = Image.blend(new_img,im,0.5) 

enhancer = ImageEnhance.Color(new_img) 
new_img = enhancer.enhance(3) 
enhancer = ImageEnhance.Contrast(new_img) 
new_img = enhancer.enhance(2) 
new_img.save("new008.png","PNG")