国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Python可交互繪圖庫"mplcursors"介紹

那么在Python中,特別是使用最常用的matplotlib庫繪制的圖像能否實(shí)現(xiàn)上述功能呢?這里為大家介紹一個(gè)可以實(shí)現(xiàn)交互式創(chuàng)建標(biāo)簽的第三方庫:"mplcursors"

01

基本介紹

mplcursors 為 Matplotlib 提供交互式數(shù)據(jù)選擇游標(biāo)。它的靈感來自 mpldatacursor,具有大大簡化的 API。

由于缺乏維護(hù),這里不建議大家使用"mpldatacursor"。

mpldatacursor的更新記錄

mplcursors的更新記錄

安裝方式為:

$ pip install mplcursors # from PyPI
$ pip install git+https://github.com/anntzer/mplcursors # from Github

在使用過程中的操作為:

  • 鼠標(biāo)左鍵單擊圖表數(shù)據(jù)元素時(shí)會(huì)彈出文本框顯示最近的數(shù)據(jù)元素的坐標(biāo)值。
  • 鼠標(biāo)右鍵單擊文本框取消顯示數(shù)據(jù)光標(biāo)。

02

示例

這里使用matplotlib和mplcursors繪制與文首Matlab繪圖相同的圖像,需要用到下述代碼:

import matplotlib.pyplot as plt
import numpy as np
import mplcursors #調(diào)用mplcursor

data = np.outer(range(10), range(1, 5))

fig, ax = plt.subplots()
ax.set_title("Multiple non-draggable annotations")
ax.plot(data)

mplcursors.cursor(multiple=True) #設(shè)置多個(gè)游標(biāo)

plt.show()
  • 多游標(biāo)功能:

  • 可以使用鼠標(biāo)對(duì)游標(biāo)進(jìn)行拖動(dòng):

  • 可以單獨(dú)為每一個(gè)數(shù)據(jù)點(diǎn)定制游標(biāo):

import matplotlib.pyplot as plt
import mplcursors
import numpy as np

labels = ["a", "b", "c", "d", "e"]
x = np.array([0, 1, 2, 3, 4])

fig, ax = plt.subplots()
line, = ax.plot(x, x, "ro")
mplcursors.cursor(ax).connect(
    "add", lambda sel: sel.annotation.set_text(labels[sel.index]))

plt.show()

  • 柱狀圖也可以添加游標(biāo):

import string
import matplotlib.pyplot as plt
import mplcursors

fig, ax = plt.subplots()
ax.bar(range(9), range(1, 10), align="center")
labels = string.ascii_uppercase[:9]
ax.set(xticks=range(9), xticklabels=labels, title="Hover over a bar")

# With HoverMode.Transient, the annotation is removed as soon as the mouse
# leaves the artist. Alternatively, one can use HoverMode.Persistent (or True)
# which keeps the annotation until another artist gets selected.
cursor = mplcursors.cursor(hover=mplcursors.HoverMode.Transient)
@cursor.connect("add")
def on_add(sel):
    x, y, width, height = sel.artist[sel.index].get_bbox().bounds
    sel.annotation.set(text=f"{x+width/2}: {height}",
                       position=(0, 20), anncoords="offset points")
    sel.annotation.xy = (x + width / 2, y + height)

plt.show()

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
python繪制三維圖
matplotlib.pyplot中繪畫操作
python如何設(shè)置中文界面
使用Python繪制二元函數(shù)圖像
Python 使用matplotlib畫圖添加標(biāo)注、及移動(dòng)坐標(biāo)軸位置
matplotlib作圖之注釋
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服