【问题标题】:how to make several plots in one figure?如何在一个图中制作多个图?
【发布时间】:2021-06-19 17:04:05
【问题描述】:

我制作了一个绘制图形的 matlab 函数。我需要对“类型”给出的数据进行分组,并绘制交易数量的图表,在给定数据中将其命名为“金额”。这就是我现在下面的内容。

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data=pd.read_csv 
('https://raw.githubusercontent.com/soulJ90/RPythonClass/main/credit.txt', 
sep=" ")
grouped=data.groupby('type')
data[data.amount.isnull()]
na_indexs=data[data.amount.isnull()].index
fill_value=data.groupby("type").amount.median()
result = pd.DataFrame()
for name, group in data.groupby('type'):
     c = group.fillna(group.median())
result = pd.concat([result,c])
fig, ax= plt.subplots()
for name, group in result:
    ax.plot(group.date,group.amount,label=name)
plt.figure(figsize=(14,6))
ax.legend(loc='upper right',ncol=2, fontsize=11)
plt.xlabel("date")
plt.ylabel("the number of transactions")
plt.title("the number of credic card transactions", fontsize=15)
plt.show()

我需要做一个如下图。但目前它不起作用。你能帮帮我吗?enter image description here

【问题讨论】:

    标签: python matplotlib subplot


    【解决方案1】:

    试试:

    import matplotlib
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    data=pd.read_csv('https://raw.githubusercontent.com/soulJ90/RPythonClass/main/credit.txt', sep=" ")
    ax = data.groupby(['date', 'type'])['amount'].count().unstack().plot(figsize=(12,8))
    ax.legend(loc='upper right')
    ax.set_ylabel('Number of Transactions')
    

    输出:

    详情:

    使用 groupby 创建 x 轴和 y 轴,然后取消堆叠该多重索引以在数据框中创建代表系列(“类型”)的列。使用 pandas.DataFrame.plot 根据数据框中的列创建具有多行(系列)的图形。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      相关资源
      最近更新 更多