本文介绍了matplotlib:当值超过阈值时不同的标记颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 matplotlib 的新手,这里是情况的简化图:http://postimg.org/image/qkdm6p31p/

Iam new to matplotlib and here is an image of simplified plot of the situation:http://postimg.org/image/qkdm6p31p/

对于高于某个阈值的值,我想有一个红色标记,在这种情况下,红线上方的两个点有一个红色标记.在 matplotlib 中可以吗?

I would like to have a red marker for the values above a certain threshold value, in this case, the two points above the red line to have a red marker. Is it possible in matplotlib?

我真的不明白为什么当我关闭窗口时我的代码永远不会完成执行.

And I really don't get why my code never completes execution when I close the window.

这是我的代码:

import wx
import numpy as np
import matplotlib
matplotlib.use('WXAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

class GraphFrame(wx.Frame):

    def __init__(self):
        self.displaySize = wx.DisplaySize()
        wx.Frame.__init__(self, None, -1,
                 style = wx.DEFAULT_FRAME_STYLE,
                 size = (self.displaySize[0], self.displaySize[1]))
        self.threshold = 3000
        self.create_main_panel()
        self.draw_plot()

    def create_main_panel(self):
        self.panel = wx.Panel(self,-1, style = wx.SUNKEN_BORDER)
        self.fig = plt.figure()
        self.canvas = FigureCanvas(self.panel, -1, self.fig)

        self.panelsizer = wx.BoxSizer(wx.HORIZONTAL)
        self.panelsizer.Add(self.canvas, 1, wx.EXPAND)
        self.panel.SetSizer(self.panelsizer)
        mainsizer = wx.BoxSizer(wx.VERTICAL)
        mainsizer.Add(self.panel, 1, wx.EXPAND )
    self.SetSizerAndFit(mainsizer)

        self.init_plot()

    def init_plot(self):
        self.axes = self.fig.add_subplot(111)
        self.axes.set_axis_bgcolor('white')
        self.axes.set_title('TITLE', size=12)
        self.data = ['2000','2869','4694','2356','3600','1500']
    self.xmin = 0
    self.xmax = len(self.data)

    def draw_plot(self):
        self.plot_data = self.axes.plot(
          self.data,
          linewidth=3,
          label = "plot1",
          marker = "o",
          markersize =7,
          )[0]
        self.plot_data.set_xdata(np.arange(len(self.data)))
        self.plot_data.set_ydata(np.array(self.data))
        thresholdplot = self.axes.plot([self.xmin,self.xmax], [self.threshold,self.threshold],"r--",label = "threshold",linewidth = 1)
        lg=self.axes.legend(loc="upper left", bbox_to_anchor=(1,1),ncol=1)
        self.canvas.draw()

if __name__ == "__main__":
  app = wx.PySimpleApp()
  app.frame = GraphFrame()
  app.frame.Show()
  app.MainLoop()
  print "Finished"

我正在使用 Matplotlib、wx 和 Python 2.7非常感谢您的帮助.

I am using Matplotlib, wx with Python 2.7Would really appreciate your help.

推荐答案

也许实现此效果的最简单方法是再次绘制大于阈值的数据子集.例如:

Perhaps the easiest way to accomplish this effect would be to simply plot the subset of your data that is greater than the threshold value a second time. For example:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

ys = np.random.rand(10)

threshold = 0.5

ax.axhline(y=threshold, color='r', linestyle=':')
ax.plot(ys)

greater_than_threshold = [i for i, val in enumerate(ys) if val>threshold]
ax.plot(greater_than_threshold, ys[greater_than_threshold],
        linestyle='none', color='r', marker='o')

plt.show()

这篇关于matplotlib:当值超过阈值时不同的标记颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:03