python - 如何在 Python 中获得漂亮的 API Plot?

标签 python api matplotlib plot

我有一个算法可以自动在我的 GUI 的绘图区域上绘制数据。有我用来这样做的代码:

self.figure = Figure(figsize=(5,4), dpi=100)
self.graph = self.figure.add_subplot(111)
self.canvas = FigureCanvasTkAgg(self.figure, master=self.root)
self.canvas.show()
self.canvas.get_tk_widget().pack(side=LEFT, fill=BOTH, expand=1)
self.canvas._tkcanvas.pack(fill=BOTH, expand=1)

下面是我如何将相关点显示到绘图区域:

if eigenvalue > self.eigenvalueThreshold:
  if self.previousEigenvalue <= self.eigenvalueThreshold:
    main.graph.scatter(windowNumber, eigenvalue, c="red")
  else:
    main.graph.scatter(windowNumber, eigenvalue, c="yellow")
else:
  main.graph.scatter(windowNumber, eigenvalue, c="blue")
  self.previousEigenvalue = eigenvalue
  main.canvas.show()

我得到这样的结果,其中缩放是根据数据点集的权重自动完成的:

http://hpics.li/14e5717

我想知道是否有机会像这样绘制我的数据:

enter image description here

你有什么想法吗?

最佳答案

对于您想要的,使用 fill_between 比使用 fill 更容易。如果您使用 fill,您将不得不担心添加顶点以创建闭合多边形。 fill_between 自动填充数据和常数值(默认为 0)或另一条曲线之间的区域。

例如:

import matplotlib.pyplot as plt
import numpy as np

# Generate some data to plot...
x = np.arange(1000)
y = np.random.normal(0, 1, y.size).cumsum()
y[y < 0] *= -1

fig, ax = plt.subplots()
# Fill the region between your curve and 0 with red...
ax.fill_between(x, y, facecolor='red', edgecolor='none')

# Optional...
ax.grid(True, color='white', linewidth=2, linestyle='-')
ax.set(axisbelow=True, axis_bgcolor='lightgray')
ax.tick_params(direction='out')

plt.show()

enter image description here

关于python - 如何在 Python 中获得漂亮的 API Plot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25427127/

相关文章:

python - 掩盖等高线图中的特定区域?

python - For Loop 使用 Pandas 创建包含分支数据的数据集

python - tf.compat.v1.random_normal() 中 random_normal 的平均值和标准差是什么意思?

python - 在 WSGI 环境中启动一个单独的线程

delphi - 如何从外部进程获取进程环境 block (PEB)?

拉拉维尔 7 : Make failed validation response 200 instead of 422

python - 在Python中使用弱引用的正确方法

python - 使用 python 的 HP QC REST API

python - canvas.restore_region() 不工作

python - 如何更改 Matplotlib 中颜色条刻度标签的字体大小?