Python 使用matplotlib获取绘图

Python 使用matplotlib获取绘图,python,python-3.x,python-2.7,function,matplotlib,Python,Python 3.x,Python 2.7,Function,Matplotlib,上面的代码正在运行,但太复杂了,无法理解。有人能给我推荐另一种写作方法吗?我是python新手,有人能帮忙吗。只有两条建议: 使用一个变量来保持ax[x\u plot][y\u plot],这样您就不必每次都重复这个过程。e、 g.:: def get_plot(x_plot, y_plot, column_span, title, x_axis_1, y_axis_1, x_axis_2 = None, x_axis_3 = None, y_ax

上面的代码正在运行,但太复杂了,无法理解。有人能给我推荐另一种写作方法吗?我是python新手,有人能帮忙吗。

只有两条建议:

使用一个变量来保持ax[x\u plot][y\u plot],这样您就不必每次都重复这个过程。e、 g.::

def get_plot(x_plot, y_plot, column_span, title, x_axis_1, y_axis_1,
            x_axis_2 = None, x_axis_3 = None,
            y_axis_2 = None, y_axis_3 = None,
            label_1 = None, label_2 = None, label_3 = None):
    ax[x_plot][y_plot] = plt.subplot2grid((3, 3), (x_plot, y_plot), colspan = column_span)
    if label_1 is None:
        ax[x_plot][y_plot].plot(x_axis_1, y_axis_1, linewidth = 2, color = ([0.37, 0.23, 0.37]), marker = 'o')
    else:
        ax[x_plot][y_plot].plot(x_axis_1, y_axis_1, linewidth = 2, color = ([0.37, 0.23, 0.37]), label = label_1, marker = 'o')

    if x_axis_2 is not None and  y_axis_2 is not None and label_2 is not None:
        ax[x_plot][y_plot].plot(x_axis_2, y_axis_2, linewidth = 2, color = ([0.44, 0.64, 0.69]), label = label_2, marker = 'o')
        if title == ' sterillite' or title == 'Fennel seeds':
            ax[x_plot][y_plot].set_ylim(0, 100)
        ax[x_plot][y_plot].legend()

    if x_axis_3 is not None and  y_axis_3 is not None and label_3 is not None:
        ax[x_plot][y_plot].plot(x_axis_3, y_axis_3, linewidth = 2, color = ([0.68, 0.74, 0.22]), label = label_3, marker = 'o')
        ax[x_plot][y_plot].legend()

    ax[x_plot][y_plot].set_xlim(xmin = 0.0)
    ax[x_plot][y_plot].yaxis.set_tick_params(labelsize = 8)
    ax[x_plot][y_plot].xaxis.set_tick_params(labelsize = 8)
    ax[x_plot][y_plot].set_axisbelow(True)
    ax[x_plot][y_plot].yaxis.grid(True)
    ax[x_plot][y_plot].xaxis.grid(False)
    ax[x_plot][y_plot].yaxis.set_major_formatter(FuncFormatter(format_y_tick_suffix))
    ax[x_plot][y_plot].set_title(title, fontsize = 10, fontweight = "bold")
不要使用
x\u axis\u 1
等等,而是创建一个保存绘图数据的结构,并将其作为列表给出:

a = ax[x_plot][y_plot] = plt.subplot2grid((3, 3), (x_plot, y_plot), colspan = column_span)
a.plot(...)
#etc.
。。。然后用一个循环来绘制图

代码(未测试):

这样打电话(根据您的评论):


解释你的想法want@DejanMarić上述代码正在运行,但太复杂了,无法理解。有人能给我推荐另一种写作方法吗?我是python新手,有人能帮我吗?谢谢你的回复,我正在考虑如何调用这个函数。你能建议我如何根据你的代码
get\u图调用这个函数吗(x_plot=0,y_plot=0,col=2,m1=hp_激光,m2=hp_键盘,m3=hp_鼠标,n1=hp_激光,n2=hp_键盘,n3=hp_鼠标,lab1='Current',lab2='Last year',lab3='2年前',title='hp_配件')获取_plot(x_plot=0,y_plot=2,col=1,title='hp_cpu,m1=hp_cpu,hp_数据点)获取_plot(m=2,n=2,col=1,title='hp_主板',m1=hp_处理器,m2=hp_内存,n1=hp_处理器,n2=hp_内存,lab1='处理器',lab2='内存')
我真的很欣赏上面的想法吗?请参阅添加的调用示例
from collections import namedtuple
PlotData = namedtuple('PlotData', 'x_axis y_axis label color')

# ...

plotdata_list = [Plotdata(x1, y1, label1, color21), ...]
get_plot(x_axis, y_axis, column_span, title, plotdata_list)
def get_plot(x_plot, y_plot, column_span, title, plotdata_list):
    a = ax[x_plot][y_plot] = plt.subplot2grid((3, 3), (x_plot, y_plot), colspan = column_span)

    for data in plotdata_list:
        kwargs = {}
        if data.label:
            kwargs['label'] = data.label
        a.plot(data.x_axis, data.y_axis, linewidth=2, color=data.color, marker='o', **kwargs)

    if len(plotdata_list) > 1:
        if title == ' sterillite' or title == 'Fennel seeds':
            a.set_ylim(0, 100)
        a.legend()

    a.set_xlim(xmin = 0.0)
    a.yaxis.set_tick_params(labelsize = 8)
    a.xaxis.set_tick_params(labelsize = 8)
    a.set_axisbelow(True)
    a.yaxis.grid(True)
    a.xaxis.grid(False)
    a.yaxis.set_major_formatter(FuncFormatter(format_y_tick_suffix))
    a.set_title(title, fontsize = 10, fontweight = "bold")
get_plot(0, 0, column_span=2, title='hp_accessories', 
        plotdata_list=[
            # arguments: x, y, label, color
            PlotData(hp_laser_ts, hp_laser, 'Lasers', [0.37, 0.23, 0.37]),
            PlotData(hp_keyboard_ts, hp_keyboard, 'Keyboards', [0.44, 0.64, 0.69]),
            PlotData(hp_mouse_ts, hp_mouse, 'Mice', [0.68, 0.64, 0.22]),
        ]
)