python - 从图表区域

时间:2017-07-18 17:26:20

标签: python matplotlib

我正在同一张图表上绘制双条形图和折线图。对于第一个和最后一个x-tick,图表中只能看到一个条形图。中间的所有x-ticks都有双条(我想要)。 image here

这是我正在使用的代码:

for h in range(0,len(x)):   # len(x) is > 4000
    if h > 0 :
        fig = plt.figure()
        ax1 = fig.add_subplot(111)   
        ax1.set_xlim([-1,13])          
        ax2 = ax1.twinx()
        df2[['A','B']].plot(kind='bar', ax=ax1, figsize=(15, 10), legend=False)
        df2[['C']].plot(style='r-', ax=ax2, figsize=(15, 10), marker='o',legend=False)

        ax1.set_xlabel("x-label", fontsize=15)
        ax1.set_ylabel("y-label 1", fontsize=15)
        ax2.set_ylabel("y-label 2", fontsize=15)

        # h1, l1 = ax1.get_legend_handles_labels()
        # h2, l2 = ax2.get_legend_handles_labels()

        plt.tight_layout()
        plt.grid()
        fig.savefig('xyz.png')
        plt.close()

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

更改x限制,两者都应显示:

function createMyClass2<MYTYPE>(type: typeof ArrayClass, element: MYTYPE): ArrayClass<MYTYPE>;
function createMyClass2<MYTYPE>(type: typeof SomeOtherArrayClass, element: MYTYPE): SomeOtherArrayClass<MYTYPE>;
function createMyClass2(className, element) {
    return new className(element);
}

let b = createMyClass2(ArrayClass, "hop!");  // type: {ArrayClass<string>}
b.test();
let c = createMyClass2(ArrayClass, 123);  // type: {ArrayClass<number>}
c.test();
let d = createMyClass2(SomeOtherArrayClass, "hop!");  // type: {SomeOtherArrayClass<string>}
d.test();

答案 1 :(得分:0)

创建绘图后需要设置xlimits。

for h in range(0,len(x)):   # len(x) is > 4000
    if h > 0 :          
        fig, ax1 = plt.subplots()   
        ax2 = ax1.twinx()
        df2[['A','B']].plot(kind='bar', ax=ax1, figsize=(15, 10), legend=False)
        df2[['C']].plot(style='r-', ax=ax2, figsize=(15, 10), marker='o',legend=False)

        ax1.set_xlabel("x-label", fontsize=15)
        ax1.set_ylabel("y-label 1", fontsize=15)
        ax2.set_ylabel("y-label 2", fontsize=15)

        ax1.set_xlim((-1,13))  # <-----   here

        plt.tight_layout()
        plt.grid()
        fig.savefig('xyz.png')
        plt.close()