在条形图条上重复填充图像

在条形图条上重复填充图像

本文介绍了在条形图条上重复填充图像(条形自定义)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何像图案一样用图像填充条形图的条形?

How can I fill bar graph's bars with image like the pattern does?

以下是我的发现或相关问题:

The following are my discoveries or related questions:

到目前为止,我找到了这篇有用的帖子.它可以帮助我输出类似的图形.这已经非常接近我想要的了.

So far I have find this useful post. It helps me to output a graph like this. This is already very near to what I want.

但是,我希望具有这样的效果,例如这篇文章.不幸的是,这是一篇JS帖子.我只想在图像达到1时用该栏填充,相应地当图像达到2时再填充两次.对于小数点,它会裁剪图像或只是调整其大小以适合条形.

However, I would like to have this effect like this post. Unfortunately, this is a JS post. I just want to fill the bar with images once it reaches 1, fill twice when it reaches 2 accordingly. And for decimal, it crops the image or just resize it to fit the bar.

这是我的游乐场代码.

from matplotlib.ticker import FuncFormatter
import matplotlib.pyplot as plt
import numpy as np
import imageio
import math

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)):
        width = 1
        left = width*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)*2)

data = {"success": True,
        "message": {
                "portion": "100g",
                "nickname": "",
                "trans-fat(g)": "NA",
                "carbohydrates(g)": "42",
                "type": "breakfast",
                "sugar(g)": "14",
                "energy(kcal)": "260",
                "fat(g)": "7.3",
                "fiber(g)": "1.6",
                "cholesterol(mg)": "17",
                "protein(g)": "7.3",
                "Na(mg)": "290",
                "name": "Pork Burger"
                }
        }
msg = data["message"]
x = np.arange(5)
values = [msg["energy(kcal)"], msg["protein(g)"], msg["fat(g)"], msg["sugar(g)"], msg["Na(mg)"]]
values = list(map(float, values))
compare = [457.0,4.68,33.15,34.98,196.38]
values[0] = values[0]/compare[0]
values[1] = values[1]/compare[1]
values[2] = values[2]/compare[2]
values[3] = values[3]/compare[3]
values[4] = values[4]/compare[4]

label = ['energy(kcal)', 'protein(g)', 'fat(g)', 'sugar(g)', 'sodium(mg)']
demaeitcho_img = imageio.imread('./img/damaeitcho.png')
soymilk_img = imageio.imread('./img/soymilk.png')
beefpho_img = imageio.imread('./img/beefpho.png')
coke_img = imageio.imread('./img/coke.png')
luncheonmeat_img = imageio.imread('./img/luncheonmeat.png')

imgs = [demaeitcho_img,soymilk_img,beefpho_img,coke_img,luncheonmeat_img]
image_plot(values, imgs, spacing=0)
plt.xticks(x, ("DemaeItcho","SoyMilk","BeefPho","Coke","LuncheonMeat"), color='orange')
  1. 是否需要为每张图片调用"plt.imshow()"?
  2. 是否必须使所有图像都具有相同的大小才能在条形图上执行更好的纹理映射?

推荐答案

经过对该方法的调查,要生成理想的图形将多次调用 plt.imshow(感谢 ImportanceOfBeingErnest 的提示)

After an investigation on the method, to produces the ideal graph would be calling the plt.imshow for multiple times (Thanks for ImportanceOfBeingErnest's hint)

所以,结果是第一位的.

点我​​看效果.

So, result comes first.

Click me to see the effect.

要执行此操作,我们需要一个while循环或for循环来计算执行imshow()所需的次数.

To do this, we need a while loop or a for loop to count how many times we need to do the imshow().

......
for i, (height, img) in enumerate(zip(heights, images)):
    ......
    count = 0
    while(count<= var_each_image_count):
#                        image l.side,r.side, b.side,  t.side
        plt.imshow(img, extent=[left, right, (count), (count+1)])
        count = count + 1
......

记住不要使用 plt.bar(),因为它会在图像上覆盖一个新的纯色条.

但是,使用这种方法似乎不能支持 CROP,因为裁剪图像需要图像上的特定坐标才能进行裁剪.

Remember do not use plt.bar(), because it will cover a new solid color bar over the images.

However, using this method seems to be not OK to support CROP as cropping an image requires specific coordinates on the image to crop.

这篇关于在条形图条上重复填充图像(条形自定义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:24