使用Matplotlib绘制多条同时出现的Python直方图

2024-06-25 22:57:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用文件中的数据创建了一个不同的直方图。现在我想把这些图合并成一个直方图。下面是生成不同直方图的代码。你知道吗

import pandas as pd
import matplotlib.pyplot as plt
import collections
import numpy as np

df  = pd.read_excel ('C:/Users/rajive/Desktop/Final_code/MSCN1.xlsx')
df1 = pd.read_excel ('C:/Users/rajive/Desktop/Final_code/MSCN2.xlsx')
df2 = pd.read_excel ('C:/Users/rajive/Desktop/Final_code/MSCN3.xlsx')
df3 = pd.read_excel ('C:/Users/rajive/Desktop/Final_code/MSCN4.xlsx')

def calculate_pair_product_coefficients () :
    return collections.OrderedDict ({
        'mscn1' : df,
        'mscn2' : df1,
        'mscn3' : df2,
        'mscn4' : df3,
#       'mscn1' : mscn_coefficients, 
        })

def plot_histogram (x, label) :
    n, bins = np.histogram (x.ravel (), bins = 100)
    n = n / np.max (n)

plt.plot (bins [:-1], n, color = 'green', label = label, marker = 'o')

coefficients = calculate_pair_product_coefficients ()
plt.rcParams ["figure.figsize"] = 12, 11

for name, coeff in coefficients.items () :
    plot_histogram (coeff.values.ravel (), name)
    plt.axis ([-2, 2, 0, 1.05])
    plt.legend ()
    plt.show ()

Tags: importreadasnpcodeplt直方图xlsx