如何在同一代码中创建两个图形?

时间:2019-02-10 09:37:09

标签: python

我想用相同的图形绘制两个不同的图形:

def Plot_data_Analysis_Regression_Line_CI(List_info,path_plot,re_key,plots):
    plt.figure(figsize=[40, 20])
    plt.suptitle(str(re_key) + '_ With Clock Drift=[-10,+10]')
    plt.subplots_adjust(hspace=2)
    nsources = len(plots.keys())
    print(nsources)
    count = 0
    for key_1, val in plots.items():
        list_Rs = []
        print(key_1)
        count += 1
        plt.subplot(nsources, 1, count)
        plt.xlabel('Timestamp')
        plt.ylabel(str(re_key[:10]))
        plt.title('Source : {}'.format(key_1)[:-14])
        for key, val in plots[key_1].items():
            plt.plot(val, range(1, len(val) + 1), marker='o', label=key)
            plt.legend(title='Destination', loc='right', prop={'size': 2})
            slope, intercept, r_value, px, serr = scipy.stats.linregress(val, list(range(1, len(val) + 1)))
            list_Rs.append(slope)
        print(list_Rs)
        Check_Distribution(list_Rs, key_1)
    plt.savefig(str(path_plot) + '/' + str(re_key) + 'Data.png')
    plt.close()

def Check_Distribution(list_Rs, key_1):

    f = plt.figure(1)
    list_Rs = []
    plt.hist(list_Rs, 100)
    plt.title("Histogram Distribution of" +str(key_1)[:-14])
    plt.xlabel("Slope")
    plt.ylabel("Number of occurrences")
    plt.savefig(str(path_plot) + '/' + str(re_key) + 'Histogram.png')
    f.show()

我想绘制两个不同的子图,第一个对每个key_1给出一个直方图。第二个图显示了我的数据输出。 但是它只给我直方图输出。

1 个答案:

答案 0 :(得分:0)

最好的方法是生成轴对象并使用轴方法来绘制数据。

例如,对于两个共享x轴的子图:

import matplotlib.pyplot as plt
import numpy as np


def plot_data(ax, x, y):

    ax.plot(x, y)
    ax.set_xlabel('x')
    ax.set_ylabel('y')


fig, (ax1,ax2) = plt.subplots(2, sharex=True)

time_series = np.arange(0, 10.1, 0.1)

plot_data(ax1, time_series, time_series)
plot_data(ax2, time_series, 2*time_series)

这是输出图形,同一图中的两个轴。

enter image description here

虽然只是一个简单的示例,但希望您可以对此进行扩展以使其适用于您的代码。