python - 如何合并熊猫中的两个地块?
问题描述
我想合并两个图,这是我的数据框:
df_inc.head()
id date real_exe_time mean mean+30% mean-30%
0 Jan 31 33.14 43.0 23.0
1 Jan 30 33.14 43.0 23.0
2 Jan 33 33.14 43.0 23.0
3 Jan 38 33.14 43.0 23.0
4 Jan 36 33.14 43.0 23.0
我的第一个情节:
df_inc.plot.scatter(x = 'date', y = 'real_exe_time')
然后
我的第二个情节:
df_inc.plot(x='date', y=['mean','mean+30%','mean-30%'])
当我尝试合并时:
fig=plt.figure()
ax = df_inc.plot(x='date', y=['mean','mean+30%','mean-30%']);
df_inc.plot.scatter(x = 'date', y = 'real_exe_time', ax=ax)
plt.show()
我得到以下信息:
我怎样才能以正确的方式合并?
解决方案
您不应将平均值作为额外的列重复。df.plot()
对于分类数据,将根据索引绘制 - 因此您会看到原始散点图(也根据索引绘制)挤在左角。您可以创建一个额外的聚合数据框,然后将其绘制到同一个图中:
import matplotlib.pyplot as plt
import pandas as pd
#test data generation
import numpy as np
n=30
np.random.seed(123)
df = pd.DataFrame({"date": np.random.choice(list("ABCDEF"), n), "real_exe_time": np.random.randint(1, 100, n)})
df = df.sort_values(by="date").reindex()
#aggregate data for plotting
df_agg = df.groupby("date")["real_exe_time"].agg(mean="mean").reset_index()
df_agg["mean+30%"] = df_agg["mean"] * 1.3
df_agg["mean-30%"] = df_agg["mean"] * 0.7
#plot both into the same subplot
ax = df.plot.scatter(x = 'date', y = 'real_exe_time')
df_agg.plot(x='date', y=['mean','mean+30%','mean-30%'], ax=ax)
plt.show()
您还可以考虑使用seaborn,例如,用于分类数据聚合的点图。
推荐阅读
- powershell - 在 PowerShell 中使用 WinSCP .NET 程序集监控 FTP 服务器上上传的文件
- c# - System.Net.WebResponse 抛出“System.Net.WebException:操作已超时”但已连接到 Web 服务器
- dialogflow-es - 有没有办法在 Dialogflow Messenger 中显示视频
- javascript - 如何在 Angular 7 项目中传递一个响应另一个请求?
- node.js - 从 nodejs 服务器流式传输复杂数组作为对 http post 的响应
- angular - Angular 7 路由路径打开源图
- java - 来自 ManyToOne 惰性关系的实体未加载到不同的类中
- c# - 在 ASP.NET Core 3.1 中,如何在未来的特定日期和时间安排具有托管服务的后台任务(Cron 作业)?
- firebase - 无效的`Podfile`文件:无效的`RNFBDatabase.podspec`文件:没有这样的文件或目录@ rb_sysopen - ../app/package.json - 反应原生
- mysql - 根据引用同一表的视图从表中删除行