绘制多个信号的最pythonic方式 [英] Most pythonic way to plot multiple signals

查看:43
本文介绍了绘制多个信号的最pythonic方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个或多个信号绘制到一个图中.

对于每个信号,可以指定单独的颜色、线宽和线型.如果必须绘制多个信号,则还应提供一个图例.

到目前为止,我使用以下代码最多可以绘制三个信号.

  import matplotlibfig = matplotlib.figure.Figure(figsize=(8,6))子图= fig.add_axes([0.1,0.2,0.8,0.75])Signal2,Signal3,图例,t =无,无,无,无Signal1 = subplot.plot(xDataSignal1,yDataSignal1,color = LineColor [0],linewidth = LineWidth [0],linestyle = LineStyle [0])if(yDataSignal2!= []和yDataSignal3!= []):Signal2, = subplot.plot(xDataSignal2, yDataSignal2, color=LineColor[1], linewidth=LineWidth[1],linestyle=LineStyle[1])Signal3,= subplot.plot(xDataSignal3,yDataSignal3,color = LineColor [2],linewidth = LineWidth [2],linestyle = LineStyle [2])Legend = subplot.legend([Signal1, Signal2, Signal3], [yLabel[0], yLabel[1], yLabel[2]],LegendPosition,labelspacing=0.1, borderpad=0.1)Legend.get_frame().set_linewidth(0.5)对于legend.get_texts() 中的t:t.set_fontsize(10)elif(yDataSignal2!= []):Signal2,= subplot.plot(xDataSignal2,yDataSignal2,color = LineColor [1],linewidth = LineWidth [1],linestyle = LineStyle [1])图例 = subplot.legend([Signal1, Signal2], [yLabel[0], yLabel[1]], LegendPosition,labelspacing=0.1, borderpad=0.1)Legend.get_frame().set_linewidth(0.5)对于legend.get_texts() 中的t:t.set_fontsize(10)

是否仍然可以通过使用matplotlib和subplot来推广该代码,使其更具Python风格并支持多达n个信号?

任何建议都将受到高度赞赏.

解决方案

字典列表可能是一个很好的解决方案(您甚至可以使用 defaultdict 来默认颜色和线宽)你不想指定它,阅读更多

I would like to plot one ore more signals into one plot.

For each signal, a individual color, linewidth and linestyle may be specified. If multiple signals have to be plotted, a legend should be provided as well.

So far, I use the following code which allows me to plot up to three signals.

import matplotlib
fig = matplotlib.figure.Figure(figsize=(8,6))
subplot = fig.add_axes([0.1, 0.2, 0.8, 0.75])
Signal2, Signal3, legend, t = None, None, None, None
Signal1, = subplot.plot(xDataSignal1, yDataSignal1, color=LineColor[0], linewidth=LineWidth[0],linestyle=LineStyle[0])
if (yDataSignal2 != [] and yDataSignal3 != []):
    Signal2, = subplot.plot(xDataSignal2, yDataSignal2, color=LineColor[1], linewidth=LineWidth[1],linestyle=LineStyle[1])
    Signal3, = subplot.plot(xDataSignal3, yDataSignal3, color=LineColor[2], linewidth=LineWidth[2],linestyle=LineStyle[2])
    legend = subplot.legend([Signal1, Signal2, Signal3], [yLabel[0], yLabel[1], yLabel[2]],LegendPosition,labelspacing=0.1, borderpad=0.1)
    legend.get_frame().set_linewidth(0.5)
    for t in legend.get_texts():
        t.set_fontsize(10)
elif (yDataSignal2 != []):
    Signal2, = subplot.plot(xDataSignal2, yDataSignal2, color=LineColor[1], linewidth=LineWidth[1],linestyle=LineStyle[1])
    legend = subplot.legend([Signal1, Signal2], [yLabel[0], yLabel[1]], LegendPosition,labelspacing=0.1, borderpad=0.1)
    legend.get_frame().set_linewidth(0.5)
    for t in legend.get_texts():
        t.set_fontsize(10)

Is it possible to generalize that code such that it is more Pythonic and supports up to n signals by still making use of matplotlib and subplot?

Any suggestions are highly appreciated.

解决方案

A list of dicts might be a good solution for this (you could even use a defaultdict to default the color and linewidth in case you don't want to specify it, read more here)

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

mysignals = [{'name': 'Signal1', 'x': np.arange(10,20,1),
             'y': np.random.rand(10), 'color':'r', 'linewidth':1},
            {'name': 'Signal2', 'x': np.arange(10,20,1),
             'y': np.random.rand(10), 'color':'b', 'linewidth':3},
            {'name': 'Signal3', 'x': np.arange(10,20,1),
             'y': np.random.rand(10), 'color':'k', 'linewidth':2}]

fig, ax = plt.subplots()
for signal in mysignals:
    ax.plot(signal['x'], signal['y'], 
            color=signal['color'], 
            linewidth=signal['linewidth'],
            label=signal['name'])

# Enable legend
ax.legend()
ax.set_title("My graph")
plt.show()

这篇关于绘制多个信号的最pythonic方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆