将图像应用于pyplot python条形图

时间:2016-10-16 02:01:21

标签: python image matplotlib graph

以下是我的代码片段,一切正常。只是好奇而不是具有特定颜色的显示条,可以将图像应用于条形图,例如国家标志等(请忽略我不一致的参数传递顺序)

感谢

 string a = "1000000001001000100000010100001001010100010000011";
 long int value;
 value = atoi(a.c_str());
 cout <<"converted value "<<value;

1 个答案:

答案 0 :(得分:2)

AFAIK没有内置的方法来执行此操作,尽管matplotlib确实允许在条形图中使用阴影线。例如,请参阅hatch_demo

但是以条形图的形式组合几次调用plt.imshow并不是非常困难。这是一个相当粗略的功能,可以用来制作使用图像的基本条形图,使用标志作为图像。

import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imread

def image_plot(heights, images, spacing=0):
    # Iterate through images and data, autoscaling the width to
    # the aspect ratio of the image
    for i, (height, img) in enumerate(zip(heights, images)):
        AR = img.shape[1] / img.shape[0]
        width = height * AR
        left = width*i + spacing*i
        right = left + width
        plt.imshow(img, extent=[left, right, 0, height])
    # Set x,y limits on plot window
    plt.xlim(0, right)
    plt.ylim(0, max(heights)*1.1)

# Read in flag images
usa_flag = imread('american_flag.png')
aussie_flag = imread('australian_flag.png').swapaxes(0, 1)
turkish_flag = imread('turkish_flag.png').swapaxes(0, 1)

# Make up some data about each country
usa_data = 33
aussie_data = 36
turkish_data = 27

data = [usa_data, aussie_data, turkish_data]
flags = [usa_flag, aussie_flag, turkish_flag]

image_plot(data, flags, spacing=2)

如果不对xy轴做任何想法,请返回此图。

enter image description here