gpt4 book ai didi

python - Matplotlib:散点图中的垂直线

转载 作者:太空狗 更新时间:2023-10-29 22:00:50 26 4
gpt4 key购买 nike

我在这里发布了相当多的代码,它在这篇文章的底部。该代码打开一个带有各种按钮和字段等的 tkinter GUI。它还在最底部使用 matplotlib 显示一个图形。我知道这不是最好的库,但我不知道其他人如何使用 tkinter。所以理想情况下,我想暂时坚持使用 matplotlib。

对于图表,我希望每个数据点都是从 [x, y] 坐标向下到 [x, 0] 的垂直线。显而易见的答案是使用条形图 thinknesses 为 1 的条形图,我试过这个但是绘图速度比散点图慢很多。

我一直想弄清楚的是,是否可以只使用此处使用的散点图方法,将垂直线绘制到 y=0。这可能吗?

或者我应该放弃尝试使用 matplotlib 并使用 pandas 或 PyQtGraph。如果是这种情况,是否有任何教程显示这是如何完成的?我试图找到一些但没有运气。

任何帮助将不胜感激。我正在使用使用 python 3.3 的 pyzo 包。

enter image description here

import numpy
from decimal import *
import tkinter as tk
import numpy as np
from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import ttk
import tkinter.scrolledtext as tkst
import spectrum_plot_2 as specplot
import sequencer as seq

class Plot:
def __init__(self, master, data):

self.x = np.array(data.spectrum[0])
self.y = np.array(data.spectrum[1])

# Create a container
self.frame = tk.Frame(master)

self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.line = self.ax.plot(self.x, self.y, '|')


self.canvas = FigureCanvasTkAgg(self.fig,master=master)
# self.canvas.show()
self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
self.frame.pack()

def update(self, data):
"""Updates the plot with new data"""

self.x = np.array(data.spectrum[0])
self.y = np.array(data.spectrum[1])

self.line[0].set_xdata(self.x)
self.line[0].set_ydata(self.y)

self.canvas.show()
self.frame.pack()


class Spectrum:
"""(Spectrum, String, Decimal, int) -> None
Import a spectrum from a text file
"""

def __init__(self, file, precision = 4, charge_state = None, sensetivity = 50, name='Unknown'):

self.precision = precision
self.name = name
self.file = file
self.charge_state = charge_state
self.spectrum = self.load_spec(file, precision)



def load_spec(self, file, precision):
"""(Spectrum, String) -> list
manipulate spectrum file and return a list of lists:
list[0] = [mz]
list[1] = [intensity]
"""

raw_spectrum = numpy.loadtxt(file)

# assign the spectrum to a dictionary
intensity = ['%.0f' % elem for elem in raw_spectrum[:,1]]
mz = ['%.4f' % elem for elem in raw_spectrum[:,0]]

spectrum = [mz, intensity]

for i in spectrum:
for j, elem in enumerate(i):
i[j] = round(Decimal(elem), precision)
j = 0

return [mz, intensity]


class View(ttk.Frame):
"""Main GUI class"""

def __init__(self, master = None):

self.WIDTH = 450
self.HEIGHT = 500

self.spectrum = seq.Spectrum(r'C:\MyPyProgs\Sequencer\data\s1c4b1.txt')
self.spectra = {}
self.spectra_names = []
self.filenames = []

###############################
### User editable variables ###

self.precision = IntVar(value=4, name='precision')
self.sensitivity = IntVar(value = 50, name='sensitivity')

### User editable variables ###
###############################

# Set up the main window
ttk.Frame.__init__(self, master, borderwidth=5, width=self.WIDTH, height=self.WIDTH)
self.master.resizable(FALSE, FALSE)
self.grid(column=0, row=0, sticky=(N, S, E, W))
self.columnconfigure(0, weight=1)

# Create the upper control frame
self.control_frame = ttk.Frame(self, width=self.WIDTH // 2, height=300, relief='sunken')
self.control_label = ttk.Label(self.control_frame, text="Controls", font='arial', justify='center')

# Precision controls definitions
self.precision_label = ttk.Label(self.control_frame, text="Precision: ")
self.precision_entry = ttk.Entry(self.control_frame, textvariable=self.precision)
self.precision_help_button = ttk.Button(self.control_frame, text="Help")

# Sensitivity controls definitions
self.sensitivity_label = ttk.Label(self.control_frame, text="Sensitivity")
self.sensitivity_entry = ttk.Entry(self.control_frame, textvariable=self.sensitivity)
self.sensitivity_reload = ttk.Button(self.control_frame, text="Reload")
self.sensitivity_help_button = ttk.Button(self.control_frame, text="Help")

self.analyse_known_button = ttk.Button(self.control_frame, text="Analyse From Known")

self.control_frame.grid(row=0, column=1, sticky=(N, E, S))
self.control_label.grid(column=0, row=0, columnspan=4, sticky=(N), pady=5, padx=self.WIDTH // 5)

### Grid layouts ###
# Precision controls grid
self.precision_label.grid(column=0, row=1, padx=2)
self.precision_entry.grid(column=1, row=1, padx=2)
self.precision_help_button.grid(column=3, row=1, padx=2)

# Sensitivity controls grid
self.sensitivity_label.grid(column=0, row=2, padx=2)
self.sensitivity_entry.grid(column=1, row=2, padx=2)
self.sensitivity_reload.grid(column=2, row=2, padx=2)
self.sensitivity_help_button.grid(column=3, row=2, padx=2)

self.analyse_known_button.grid(column=1, row=3, columnspan=2)


### Output frame using ScrolledText ###
self.output_frame = ttk.Frame(self, relief='sunken')
self.output_frame.grid(row=0, column=0)

self.output = tkst.ScrolledText(self.output_frame, width=45, height=20, wrap=WORD)
self.output.grid(row=0, column=0, sticky=(N, S, E, W))
self.output.see(END)

self.output.insert(END, "Welcome, before you start make sure that the backbone and sugar structures are correct. To analyse your spectra follow the steps below: \n 1. Type the known sequence into the text box from 5' to 3' and click assign. \n 2. Load your spectra in order of charge, File -> Open Spectra... . \n 3. Finally click the Analyse From Known button. \n")
self.output['state']='disabled'

### Creates a sunken frame to get the sequence and choose loaded spectra ###
self.input_frame = ttk.Frame(self, relief='sunken', borderwidth=5, width=self.winfo_width())
self.input_frame.grid(row=1, column=0, columnspan=2, sticky=(E, W))

self.spec_label = ttk.Label(self.input_frame, text="Spectrum:")
self.selected_spec = StringVar()
self.spec_select = ttk.Combobox(self.input_frame, values=self.spectra_names)

self.spec_label.grid(row=0, column=6, padx=10)
self.spec_select.grid(row=0, column=7)

seq_entry_label = ttk.Label(self.input_frame, text="Sequence: ")
label_5p = ttk.Label(self.input_frame, text="5'-")
self.sequence_entry = ttk.Entry(self.input_frame, width=40)
label_3p = ttk.Label(self.input_frame, text="-3'")
assign_seq = ttk.Button(self.input_frame, text="Calculate", command=lambda : self.assign(self.sequence_entry))

seq_entry_label.grid(row=0, column=0)
label_5p.grid(row=0, column=1)
self.sequence_entry.grid(row=0, column=2)
label_3p.grid(row=0, column=3)
assign_seq.grid(row=0, column=4)

### Creates a sunken frame to plot the current spectrum ###
self.spec_frame = ttk.Frame(self, relief='sunken', borderwidth=1, width=self.winfo_width(), height=250)
self.spec_frame.grid(row=2, column=0, columnspan=2, sticky=(S, E, W))

self.plot = specplot.Plot(self.spec_frame, self.spectrum)

precision = 4
charge = -1
file = r'C:\MyPyProgs\sequencer\data\s1c4b1.txt'
spectrum = Spectrum(file, precision, charge)


if __name__ == "__main__":
root = Tk()
root.title("Sequencer_help")
view = View(root)
root.mainloop()
print("End")

最佳答案

除了@mgilson 对vlines 的建议(它做你想做的但需要你指定底部位置),你还应该看看stem .

例如:

import matplotlib.pyplot as plt
import numpy as np

x, y = np.random.random((2, 20))

fig, ax = plt.subplots()
ax.stem(x, y)
plt.show()

enter image description here

或者省略点:

import matplotlib.pyplot as plt
import numpy as np

x, y = np.random.random((2, 20))

fig, ax = plt.subplots()
ax.stem(x, y, markerfmt=' ')
plt.show()

enter image description here

关于python - Matplotlib:散点图中的垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21192576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com