這是我第一次在這里提問,所以我希望我能正確地提出任何關于清晰度的反饋。
由于我正在使用的資料結構,我被迫在我目前正在撰寫的代碼中使用 matplotlib 的繪圖函式。但它在回圈中繪圖效果不佳。對于我的情節,我的目標是能夠修改用戶確定的背景視窗,并接受或拒絕輸出。但據我所知,matplotlibs 互動函式和回圈的使用之間存在一些沖突。我對 python 比較陌生,所以代碼可能不是最漂亮的,但通常我可以完成作業。然而,我對特定問題完全不知所措。我在互聯網上看到過類似的問題,這些問題已經用 plt.pause('timeinterval') 解決了,但在我的情況下這不是一個選項。或者至少我看不到如何使用它,因為我想等待用戶輸入。我也試過 plt.
我想第三個選擇是從 hyperspy 使用的格式中提取資料,然后制作我自己的畫布,這滿足了我的需求,但由于我缺乏經驗,這對我來說非常繁瑣。
有沒有人有任何替代方法來制作情節,最好是使用 matplotlib,這樣我就可以實作我正在嘗試的東西?
順便說一句,我也嘗試過關閉互動模式,但這并不能解決問題。
關于規格的一些資訊:這是在 windows pc 上運行的,使用 jupyterlab 和 python 3.10
我希望我的困境很清楚。
def set_background(self):
"""
This function is ment to let the user define the background of each element, and then save the background for later use
if working with multiple images of particles with the same composition.
This function could be expanded to have interactive features so background would be clickable.
"""
self.Background_tree = {}
elements_in_sample = deepcopy(self.Data.metadata.Sample['elements'])
Xray_in_sample = self.weighted_Xray_line_list
data_sample = deepcopy(self.Data)
integration_window = 1.3
for element in elements_in_sample:
data_sample.set_elements(elements=[element])
for xray_line in (self.Data.metadata.Sample["xray_lines"]):
if element in xray_line:
data_sample.set_lines([xray_line])
background_points = input('please input the background points seperated by a space').split(' ')
background_window = list(map(float,background_points))
bw = data_sample.estimate_background_windows(background_window)
iw = data_sample.estimate_integration_windows(integration_window)
data_sample.sum().plot(True,bakcground_windows=background_window)
happy = input('are you happy with the result?')
if happy == 'y':
#self.Data.get_lines_intensity(xray_lines=[xray_line], background_windows=bw, integration_windows=iw)
self.Background_tree[element "_" xray_line] = bw
uj5u.com熱心網友回復:
import pandas as pd
import numpy as np
import ipywidgets as wg
from ipywidgets import HBox, VBox
import matplotlib.pyplot as plt
from IPython.display import display
%matplotlib widget
a = np.arange(50)
b = np.random.rand(50) a
c = np.sin(a)
d = np.cos(b)
df = pd.DataFrame({'a': a,
'b': b,
'c': c,
'd': d})
userinput1 = wg.Text(value="", placeholder='Type something', description='x axis')
userinput2 = wg.Text(value="", placeholder='Type something', description='y axis')
buttonplot = wg.Button(description='Plot', disabled=False, button_style='', tooltip='Click me',icon='check')
buttonout = wg.Output()
display(HBox((userinput1, userinput2, buttonplot)))
display(buttonout)
plt.close()
fig, ax = plt.subplots()
def on_click_event(change):
with buttonout:
x = (userinput1.value)
y = (userinput2.value)
ax.plot(df[x], df[y], label=f'{y}')
ax.legend()
buttonplot.on_click(on_click_event)
輸出:

用戶輸入并單擊按鈕后:

更多用戶輸入:

它滿足您的需求還是我離您最初的問題越來越遠?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317833.html
標籤:Python matplotlib
