python组合绘画,使用Python Matplotlib组合图片和绘图

I have a plot which has timestamps on the x-axis and some signal data on the y-axis. As a documentation I want to put timestamped pictures in relation to specific points in the plot. Is it possible to draw a line in a plot to a picture in a sequence of pictures below the plot?

解决方案

This demo from the matplotlib gallery shows how to insert pictures, draw lines to them, etc. I'll post the image from the gallery, and you can follow the link to see the code.

a45d6932a1abb55aa9d9f1da46243dd1.png

And here's the code (from version 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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值