Matplotlib - 2个问题。常见颜色条/标签未显示

时间:2014-04-15 18:28:04

标签: python matplotlib plot

我最终将我想要的3个图形强加到一个带有3个子图的图中......现在我需要添加一个共同的颜色条,最好是水平方向。此外,现在我将它们作为子图,我丢失了之前迭代中的标签。

似乎这些例子提示我add an axes,但我并不完全了解参数中的数字。

def plot_that_2(x_vals, y_vals, z_1_vals, z_2_vals, z_3_vals, figname, units, efficiency_or_not):
    global letter_pic_width    
    plt.close()    #I moved this up from the end of the file because it solved my QTagg problem
    UI = [uniformity_calc(z_1_vals), uniformity_calc(z_2_vals), uniformity_calc(z_3_vals)]
    ranges = [ str(int(np.max(z_1_vals) - np.min(z_1_vals))), str(int(np.max(z_2_vals) - np.min(z_2_vals))), str(int(np.max(z_3_vals) - np.min(z_3_vals)))]
    z_vals = [z_1_vals, z_2_vals, z_3_vals]

    fig = plt.figure(figsize = (letter_pic_width, letter_pic_width/3 ))
    ax0 = fig.add_subplot(1,3,1, aspect = 1)
    ax1 = fig.add_subplot(1,3,2, aspect = 1)
    ax2 = fig.add_subplot(1,3,3, aspect = 1)

    axenames = [ax0, ax1, ax2]

    for z_val, unif, rangenum, ax in zip(z_vals, UI, ranges, axenames):
        ax.scatter(x_vals, y_vals, c = z_val, s = 100, cmap = 'rainbow')
        if efficiency_or_not:
            ax.vmin = 0
            ax.vmax = 1
            ax.xlabel = 'Uniformity: ' + unif
        else:
            ax.xlabel = 'Uniformity: ' + unif + '   ' + rangenum + ' ppm'

    plt.savefig('./'+ figname + '.jpg', dpi = 100) 

This is the plot when efficiency = True. I think it's not setting the vmin / vmax, either.

2 个答案:

答案 0 :(得分:1)

要设置xlabel,请使用ax.set_xlabel('Uniformity: ' + unif)在轴的文档中查看更多信息here

您链接到的示例使用数字的add_axes方法作为add_subplot的替代方法。 documentation for figures解释了add_axes中的数字是什么:“在位置rect [left,bottom,width,height]添加一个轴,其中所有数量都是图形宽度和高度的分数。”

rect = l,b,w,h
fig.add_axes(rect)

答案 1 :(得分:1)

要回答有关色条轴的问题,数字代表

[bottom_left_x_coord, bottom_left_y_coord, width, height]

适当的颜色栏可能是

# x    y    w     h
[0.2, 0.1, 0.6, 0.05]

这是你的代码,稍微改了一下,添加了一个颜色条:

import numpy as np
import matplotlib.pyplot as plt

WIDTH = 9

def uniformity_calc(x):
    return x.mean()

def plotter(x, y, zs, name, units, efficiency=True):
    fig, axarr = plt.subplots(1, 3, figsize=(WIDTH, WIDTH/3), 
                              subplot_kw={'aspect':1})
    fig.suptitle(name)

    UI = map(uniformity_calc, zs)
    ranges = map(lambda x: int(np.max(x)-np.min(x)), zs)

    for ax, z, unif, rangenum in zip(axarr, zs, UI, ranges):
        scat = ax.scatter(x, y, c=z, s=100, cmap='rainbow')
        label = 'Uniformity: %i'%unif
        if not efficiency:
            label += '    %i ppm'%rangenum
        ax.set_xlabel(label)

    # Colorbar [left, bottom, width, height
    cax = fig.add_axes([0.2, 0.1, 0.6, 0.05])
    cbar = fig.colorbar(scat, cax, orientation='horizontal')
    cbar.set_label('This is a colorbar')
    plt.show()


def main():
    x, y = np.meshgrid(np.arange(10), np.arange(10))
    zs = [np.random.rand(*y.shape) for _ in range(3)]
    plotter(x.flatten(), y.flatten(), zs, 'name', None)

if __name__ == "__main__":
    main()

enter image description here