2010-09-21 39 views
14

Tôi có một âm mưu có dấu thời gian trên trục x và một số dữ liệu tín hiệu trên trục y. Là một tài liệu hướng dẫn, tôi muốn đặt các hình ảnh có dấu thời gian liên quan đến các điểm cụ thể trong cốt truyện. Có thể vẽ một đường thẳng trong một âm mưu đến một bức tranh trong một chuỗi các hình ảnh bên dưới cốt truyện không?Kết hợp hình ảnh và âm mưu với Python Matplotlib

Trả lời

16

This bản trình diễn từ thư viện matplotlib cho biết cách chèn ảnh, vẽ đường cho họ, v.v. Tôi sẽ đăng hình ảnh từ thư viện và bạn có thể theo dõi link để xem mã. enter image description here

Và đây là đoạn code (từ phiên bản 2.1.2):

import matplotlib.pyplot as plt 
import numpy as np 

from matplotlib.patches import Circle 
from matplotlib.offsetbox import (TextArea, DrawingArea, OffsetImage, 
            AnnotationBbox) 
from matplotlib.cbook import get_sample_data 


if 1: 
    fig, ax = plt.subplots() 

    # Define a 1st position to annotate (display it with a marker) 
    xy = (0.5, 0.7) 
    ax.plot(xy[0], xy[1], ".r") 

    # Annotate the 1st position with a text box ('Test 1') 
    offsetbox = TextArea("Test 1", minimumdescent=False) 

    ab = AnnotationBbox(offsetbox, xy, 
         xybox=(-20, 40), 
         xycoords='data', 
         boxcoords="offset points", 
         arrowprops=dict(arrowstyle="->")) 
    ax.add_artist(ab) 

    # Annotate the 1st position with another text box ('Test') 
    offsetbox = TextArea("Test", minimumdescent=False) 

    ab = AnnotationBbox(offsetbox, xy, 
         xybox=(1.02, xy[1]), 
         xycoords='data', 
         boxcoords=("axes fraction", "data"), 
         box_alignment=(0., 0.5), 
         arrowprops=dict(arrowstyle="->")) 
    ax.add_artist(ab) 

    # Define a 2nd position to annotate (don't display with a marker this time) 
    xy = [0.3, 0.55] 

    # Annotate the 2nd position with a circle patch 
    da = DrawingArea(20, 20, 0, 0) 
    p = Circle((10, 10), 10) 
    da.add_artist(p) 

    ab = AnnotationBbox(da, xy, 
         xybox=(1.02, xy[1]), 
         xycoords='data', 
         boxcoords=("axes fraction", "data"), 
         box_alignment=(0., 0.5), 
         arrowprops=dict(arrowstyle="->")) 

    ax.add_artist(ab) 

    # Annotate the 2nd position with an image (a generated array of pixels) 
    arr = np.arange(100).reshape((10, 10)) 
    im = OffsetImage(arr, zoom=2) 
    im.image.axes = ax 

    ab = AnnotationBbox(im, xy, 
         xybox=(-50., 50.), 
         xycoords='data', 
         boxcoords="offset points", 
         pad=0.3, 
         arrowprops=dict(arrowstyle="->")) 

    ax.add_artist(ab) 

    # Annotate the 2nd position with another image (a Grace Hopper portrait) 
    fn = get_sample_data("grace_hopper.png", asfileobj=False) 
    arr_img = plt.imread(fn, format='png') 

    imagebox = OffsetImage(arr_img, zoom=0.2) 
    imagebox.image.axes = ax 

    ab = AnnotationBbox(imagebox, xy, 
         xybox=(120., -80.), 
         xycoords='data', 
         boxcoords="offset points", 
         pad=0.5, 
         arrowprops=dict(
          arrowstyle="->", 
          connectionstyle="angle,angleA=0,angleB=90,rad=3") 
         ) 

    ax.add_artist(ab) 

    # Fix the display limits to see everything 
    ax.set_xlim(0, 1) 
    ax.set_ylim(0, 1) 

    plt.show() 
+0

Liệu bất cứ ai biết nếu điều này hoạt động trong không gian 3D? chúng ta có thể thêm ảnh vào máy bay được không? – CromeX

+0

@CromeX: vui lòng đặt câu hỏi riêng. Các liên kết – tom10

+0

dường như không hoạt động nữa. –

14

Nếu tôi hiểu câu hỏi một cách chính xác, thì có lẽ điều này có thể giúp:

import scipy 
import pylab 
fig = pylab.figure() 
axplot = fig.add_axes([0.07,0.25,0.90,0.70]) 
axplot.plot(scipy.randn(100)) 
numicons = 8 
for k in range(numicons): 
    axicon = fig.add_axes([0.07+0.11*k,0.05,0.1,0.1]) 
    axicon.imshow(scipy.rand(4,4),interpolation='nearest') 
    axicon.set_xticks([]) 
    axicon.set_yticks([]) 
fig.show() 
fig.savefig('iconsbelow.png') 

alt text

+0

Khi đường viền màu đen xung quanh hình ảnh là không mong muốn, các lệnh 'axicon.set_xticks ([])' và 'axicon.set_yticks ([])' có thể được thay thế bằng 'axicon.axis ('off')'. –

Các vấn đề liên quan