在条形图条上重复填充图像(条形定制)
Fill images repeatedly on bar graph bars (bars customization)
如何像模式一样用图像填充条形图的条形?
以下是我的发现或相关问题:
到目前为止我已经找到 . It helps me to output a graph like this。这已经很接近我想要的了。
不过,我想要this post这样的效果。不幸的是,这是一个 JS post。我只想在它达到 1 时用图像填充条,当它达到 2 时相应地填充两次。对于小数,它会裁剪图像或调整图像大小以适合条形。
这是我的游乐场代码。
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')
- 是否需要为每张图片调用 "plt.imshow()"?
- 是否必须使所有图像具有相同的大小才能在条上执行更好的纹理映射?
经过对该方法的研究,要生成理想的图形将多次调用 plt.imshow(感谢 ImportanceOfBeingErnest 的提示)
所以,结果第一。
Click me to see the effect.
为此,我们需要一个 while 循环或一个 for 循环来计算我们需要执行 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(),因为它会在图像上覆盖一个新的纯色条。
但是,使用这种方法似乎不能支持裁剪图像,因为裁剪图像需要图像上的特定坐标才能裁剪。
如何像模式一样用图像填充条形图的条形?
以下是我的发现或相关问题:
到目前为止我已经找到
不过,我想要this post这样的效果。不幸的是,这是一个 JS post。我只想在它达到 1 时用图像填充条,当它达到 2 时相应地填充两次。对于小数,它会裁剪图像或调整图像大小以适合条形。
这是我的游乐场代码。
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')
- 是否需要为每张图片调用 "plt.imshow()"?
- 是否必须使所有图像具有相同的大小才能在条上执行更好的纹理映射?
经过对该方法的研究,要生成理想的图形将多次调用 plt.imshow(感谢 ImportanceOfBeingErnest 的提示)
所以,结果第一。
Click me to see the effect.
为此,我们需要一个 while 循环或一个 for 循环来计算我们需要执行 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(),因为它会在图像上覆盖一个新的纯色条。
但是,使用这种方法似乎不能支持裁剪图像,因为裁剪图像需要图像上的特定坐标才能裁剪。