当数值代表数字和颜色时绘制字典值

当数值代表数字和颜色时绘制字典值

问题描述:

字典显示多少块水果和椭圆应该是什么颜色。 我看示例代码如下所示绘制字典当数值代表数字和颜色时绘制字典值

例如: my_dict { '苹果':[20, '#2E9127'], '梨':[3, '#FB9A27'], '樱桃':[7,'#187429']}

所以这种方式的情节将显示20点的颜色#2E9127。

关键是不要在这一点上相关,但数值1的计数和v2是十六进制颜色 所以当我做elipse(graph below or click this link)我想看看20倍#2E9127,3倍#FB9A27倍和7倍#187429 。

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib.patches import Ellipse 

    NUM = len(my_dict) #but dont want total random dots so thinking this is 
          #sum of value 1 possibly in a loop 


    #example from matplotlib 
    # so clearly dont want random np values 
    ells = [Ellipse(xy=np.random.rand(2) * 10, 
        width=np.random.rand(), height=np.random.rand(), 
        angle=np.random.rand() * 360) 
      for i in range(NUM)] 

    #ME HAVING A CRACK! 
    # 
    ells = [Ellipse(xy=my_dict(2) * 10, 
        width=np.random.rand(), height=np.random.rand(), 
        angle=np.random.rand() * 360,facecolor=y) 
      for i in range(NUM)] 


    fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'}) 
    for e in ells: 
     ax.add_artist(e) 
     e.set_clip_box(ax.bbox) 
     e.set_alpha(np.random.rand()) 
     e.set_facecolor(np.random.rand(3)) 

    ax.set_xlim(0, 10) 
    ax.set_ylim(0, 10) 

    plt.show() 

enter image description here

+0

目前尚不清楚字典和省略号是如何关联。哪个椭圆应该有哪种颜色?既然你有250个椭圆,并且所有值的和只有30个,它甚至不能从这个例子中推导出来。 – ImportanceOfBeingErnest

+0

对不起,这是从matplotlib示例代码 - 我把它记下 – Aza

+0

我在想也许我更好转换为列表 func =() 为(i,j)在p: func = func +(int( i),j) print(func) – Aza

如果我理解正确的问题,你想画尽可能多的椭圆从字典中值的总和将给予。 (20个苹果,7个樱桃,3个梨)

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib.patches import Ellipse 

my_dict ={'apples': [20, '#2E9127'], 'pears': [3, '#FB9A27'], 'cherries': [7, 'crimson']} 

fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'}) 

for key, val in my_dict.items(): 
    color = val[1] 
    for i in range(val[0]): 
     el = Ellipse(xy=np.random.rand(2) * 10, 
        width=np.random.rand(), height=np.random.rand(), 
        angle=np.random.rand() * 360, color=color) 
     ax.add_artist(el) 


ax.set_xlim(0, 10) 
ax.set_ylim(0, 10) 

plt.show() 

我做红要能够清晰地看到了樱桃:

enter image description here

+0

传奇!!这是非常好的帮助,非常感谢 – Aza