Ab *_*ett 3 python image graph matplotlib
所以下面是我的代码片段,一切正常。只是好奇而不是显示具有特定颜色的条,是否可以将图像应用于条,例如国家/地区国旗等(请忽略我不一致的参数传递顺序)
谢谢
l_images=["australia.png","turkey.png"] # this is desired
l_colors=["pink","blue"]
if (l_bar_dir=="vertical"):
plt.bar(xs2,ys,tick_label=xs,color=l_colors,bottom=bottoms,width=bar_width,align='center') # set plot to be a bar graph
else:
plt.barh(bottom=xs2,width=ys,tick_label=xs,align='center',color=l_colors) # set plot to be a bar graph
Run Code Online (Sandbox Code Playgroud)
AFAIK 没有内置的方法可以做到这一点,尽管matplotlib确实允许条形图中的影线。例如,请参见hat_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)
Run Code Online (Sandbox Code Playgroud)
不对x和y轴做任何花哨的操作,返回此图。
| 归档时间: |
|
| 查看次数: |
2568 次 |
| 最近记录: |