python - 绘制从同一图形 Python 中的循环获得的两个数据帧

标签 python pandas dataframe matplotlib plot

我想绘制两个 dfs有两种不同的颜色。对于每个 df ,我需要添加两个标记。这是我尝试过的:

for stats_file in stats_files:
    data = Graph(stats_file)
    Graph.compute(data)
    data.servers_df.plot(x="time", y="percentage", linewidth=1, kind='line')
    plt.plot(data.first_measurement['time'], data.first_measurement['percentage'], 'o-', color='orange')
    plt.plot(data.second_measurement['time'], data.second_measurement['percentage'], 'o-', color='green')
plt.show()
使用这段代码,我得到 servers_df用标记绘制,但在单独的图表上。
如何将两个图表放在一个图表中以更好地比较它们?
谢谢。

最佳答案

TL;博士
您调用data.servers_df.plot()总是创建一个新的情节,plt.plot()在最新创建的地 block 上绘制地 block 。解决方案是为要绘制的所有内容创建专用轴。
前言
我假设你的变量如下

  • data.servers_df :具有两个浮点列的数据框"time""percentage"
  • data.first_measurements : 带键的字典 "time"和 `"percentage",每个都是浮点数列表
  • data.second_measurements : 带键的字典 "time""percentage" ,每个都是浮点数列表

  • 我跳过了生成 stat_files因为你没有显示Graph()确实如此,但只是创建了一个虚拟列表 data .
    如果 data.first_measurementsdata.second_measurements也是数据框,让我知道,还有一个更好的解决方案。
    理论——幕后
    每个matplotlib情节(线、条等)位于 matplotlib.axes.Axes元素。这些就像坐标系的规则轴。现在这里发生了两件事:
  • 当您使用 plt.plot() ,没有指定轴,因此,matplotlib 查找当前的轴元素(在后台),如果没有,它将创建一个空的并使用它,并设置为默认值。第二次调用plt.plot()然后找到这些轴并使用它们。
  • DataFrame.plot()另一方面,如果没有给它一个新的轴元素(可能通过 ax 参数)

  • 所以在你的代码中,data.servers_df.plot()首先在窗帘后面创建一个轴元素(这是默认设置),然后是两个 plt.plot()调用获取默认轴并在其上绘制 - 这就是为什么你得到两个图而不是一个。
    解决方案
    以下解决方案首先创建一个专用的matplotlib.axes.Axes使用 plt.subplots() .然后使用该轴元素在其上绘制所有线。特别注意 ax=axdata.server_df.plot() .请注意,我将标记的显示从 o- 更改为至o (因为我们不想显示一行( - ),而只显示标记( o ))。
    模拟数据可以在下面找到
    fig, ax = plt.subplots()  # Here we create the axes that all data will plot onto
    for i, data in enumerate(stat_files):
        y_column = f'percentage_{i}'  # Make the columns identifiable
        data.servers_df \
            .rename(columns={'percentage': y_column}) \
            .plot(x='time', y=y_column, linewidth=1, kind='line', ax=ax)
        ax.plot(data.first_measurement['time'], data.first_measurement['percentage'], 'o', color='orange')
        ax.plot(data.second_measurement['time'], data.second_measurement['percentage'], 'o', color='green')
    plt.show()
    
    enter image description here
    模拟数据
    import random
    
    import pandas as pd
    import matplotlib.pyplot as plt
    
    # Generation of dummy data
    random.seed(1)
    NUMBER_OF_DATA_FILES = 2
    X_LENGTH = 10
    
    
    class Data:
        def __init__(self):
            self.servers_df = pd.DataFrame(
                {
                    'time': range(X_LENGTH),
                    'percentage': [random.randint(0, 10) for _ in range(X_LENGTH)]
                }
            )
            self.first_measurement = {
                'time': self.servers_df['time'].values[:X_LENGTH // 2],
                'percentage': self.servers_df['percentage'].values[:X_LENGTH // 2]
            }
            self.second_measurement = {
                'time': self.servers_df['time'].values[X_LENGTH // 2:],
                'percentage': self.servers_df['percentage'].values[X_LENGTH // 2:]
            }
    
    
    stat_files = [Data() for _ in range(NUMBER_OF_DATA_FILES)]
    

    关于python - 绘制从同一图形 Python 中的循环获得的两个数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64856601/

    相关文章:

    python - 在pycharm中运行nupichelloworld

    Python:如何选择有条件的列?

    python - 在 matplotlib 中使用科学计数法时定位刻度标签的指数

    python - Snakemake:本地计算机上的SFTP

    python - 规范化 ID 列

    python - Pandas 数据框旋转

    python - 合并 Pandas 中多行的多列文本

    r - 对于 Y 列中每行的长度,X 列中重复行

    pandas - 按最大值作为索引对数据帧进行切片

    python - 删除各个子目录中相同的文件名