繁体   English   中英

将绘图传递给函数Matplotlib Python

[英]Pass Plot to Function Matplotlib Python

我希望创建一个函数来设置绘图的x轴限制。 这是当前正在工作的

import matplotlib.pyplot as plt

def scatter_plot(df, user_conditions):
    data_to_plot = user_conditions['data_to_plot']
    title = data_to_plot.replace("_", " ").title()
    df1 = df[['time',data_to_plot]]
    df1 = index_dataframe_time(df1)
    plt.scatter(df1.index.to_pydatetime(), df1[data_to_plot])
    min = df1.index.min()
    max = df1.index.max()
    plt.xlim(min, max)
    plt.title('Hour of Day vs '+title, fontsize=14)
    plt.show()

这是我一直希望的:

def scatter_plot(df, user_conditions):
    data_to_plot = user_conditions['data_to_plot']
    title = data_to_plot.replace("_", " ").title()
    print title
    df1 = df[['time',data_to_plot]]
    df1 = index_dataframe_time(df1)
    plot = plt.scatter(df1.index.to_pydatetime(), df1[data_to_plot])
    plot = set_limits(df1, plot)
    plot.title('Hour of Day vs '+title, fontsize=14)
    plot.show()

def set_limits(df, plot):
    min = df.index.min()
    max = df.index.max()
    plot.xlim(min, max)
    return plot

但是, plot.xlim(min,max)set_limits存在问题,

> Traceback (most recent call last):   
File
> "C:/Users/Application/main.py", line 115, in <module>
>     
main()   
> 
> File "C:/Users/Application/main.py", line
> 106,
> in main
>     plot_configuration(df, user_conditions)   File "C:/Users/Application/main.py", line 111,
> in plot_configuration
>     scatter_plot(df, user_conditions)   
File "C:/Users/Application/main.py", line 76,
> in scatter_plot
>     plot = set_limits(df1, plot)   File "C:/Users/Application/main.py", line 83,
> in set_limits
>     plot.xlim(min, max) AttributeError: 'PathCollection' object has no attribute 'xlim'

如何修改set_limits以解决此问题?

您可能想要做这样的事情:

import matplotlib.pyplot as plt

def scatter_plot(ax, df, user_conditions):
    """
    Parameters
    ----------
    ax : matplotlib.axes.Axes
        The axes to put the data on
    df : pd.DataFrame
        The data
    user_conditions : dict (?)
        bucket of user input to control plotting?
    """
    data_to_plot = user_conditions['data_to_plot']
    title = data_to_plot.replace("_", " ").title()
    print(title)
    df1 = df[['time',data_to_plot]]
    df1 = index_dataframe_time(df1)
    # sc = ax.scatter(df1.index.to_pydatetime(), df1[data_to_plot])
    # only works in 1.5.0+
    sc = ax.scatter(df1.index.to_pydatetime(), data_to_plot,
                    data=df)
    set_limits(df1, ax)
    ax.set_title('Hour of Day vs '+title, fontsize=14)

    return sc

def set_limits(df, ax):
    min = df.index.min()
    max = df.index.max()
    ax.set_xlim(min, max)


fig, ax = plt.subplots()
arts = scatter_plot(ax, df, user_conditions)

如果您不改变标记的大小或颜色,则最好使用ax.plot(..., linestile='none', marker='o') ,它会更快地渲染。 在这种情况下,例如(如果您有1.5.0+)

ax.plot(data_to_plot, linestyle='none', marker='o', data=df)

它应该“做正确的事”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM