Python 当值表示数字和颜色时,打印字典值
字典显示了多少片水果,以及elipse应该是什么颜色。 我正在查看如下所示的示例代码以绘制dict 例如: 我的名言{‘苹果’:[20,#2E9127'],‘梨’:[3,#FB9A27'],‘樱桃’:[7,#187429']} 这样,绘图将显示该颜色的20个点#2E9127 键在这一点上不相关,但value1是计数,v2是十六进制颜色 因此,当我制作elipse()时,我希望看到20次(2E9127),3次(FB9A27)和7次(187429)Python 当值表示数字和颜色时,打印字典值,python,dictionary,matplotlib,Python,Dictionary,Matplotlib,字典显示了多少片水果,以及elipse应该是什么颜色。 我正在查看如下所示的示例代码以绘制dict 例如: 我的名言{‘苹果’:[20,#2E9127'],‘梨’:[3,#FB9A27'],‘樱桃’:[7,#187429']} 这样,绘图将显示该颜色的20个点#2E9127 键在这一点上不相关,但value1是计数,v2是十六进制颜色 因此,当我制作elipse()时,我希望看到20次(2E9127),3次(FB9A27)和7次(187429) 如果我正确理解了这个问题,您希望绘制尽可能多的省略
如果我正确理解了这个问题,您希望绘制尽可能多的省略号,就像字典中给出的值的总和一样。(20个苹果,7个樱桃,3个梨) 我把樱桃弄红了,以便能看清它们:
不清楚词典和省略号之间的关系。哪些椭圆应该有哪些颜色?由于有250个椭圆,所有值之和只有30个,因此从示例中甚至无法推断。很抱歉,这是matplotlib中的示例代码--我记下了它。我在想,也许我最好将p:func=func+(int(i),j)print(func)中的(i,j)转换为列表func=()现在更不清楚了,因为代码丢失了。好的,1秒钟,让我编辑一下
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()
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()