我试着建立一个有两个子图的图形。在
如果项目在ax
和ax1
中,则条的颜色为蓝色。
如果项目在ax
中而不是在ax1
中,则条形图颜色应为绿色。
如果项目不在ax
中,而它在ax1
中,则条形图颜色应为红色。
所以在我的例子中:
条Exploit
应为绿色,XSS
应为红色。在
有没有一种方法可以用Matplotlib做到这一点?在
代码如下:
import matplotlib.pyplot as plt
from collections import OrderedDict
from matplotlib.backends.backend_pdf import PdfPages
from Processesing import dataProcess
from Sizeconvertor import Szconv
def chartmak (curvdic,prevdic,imgname,unit) :
Colors =[]
ImgName=imgname
D={}
D1={}
D=curvdic
D1=prevdic
if len(D):
listo=list(D.items())
listo1=list(D1.items())
if len(listo) < 10:
listo.extend([('', 0)] * (10 - len(listo)))
if len(listo1) < 10:
listo1.extend([('', 0)] * (10 - len(listo1)))
values1 = [v for l, v in listo1]
values = [v for l, v in listo]
Dict=listo
Dict1=listo1
fig, (ax,ax1) = plt.subplots(ncols=2,figsize=(12, 5))
n = len(Dict)
n1 = len(Dict1)
#--------------add the 1 barchart
ax.barh(range(n), values, align='center', fc='#80d0f1', ec='w')
ax.set_yticks(range(n))
ax.set_yticklabels(['' if e == 0 else Szconv(e,unit) for e in values], color='gray')# add the unit
ax.tick_params(pad=10)
for i, (label, val) in enumerate(Dict):
ax.annotate(label.title(), xy=(10, i), fontsize=10, va='center')
for spine in ('top', 'right', 'bottom', 'left'):
ax.spines[spine].set_visible(False)
ax.xaxis.set_ticks([])
ax.yaxis.set_tick_params(length=0)
ax.invert_yaxis()
ax.set_title('curmonth')
#----------add the secound plot
ax1.barh(range(n1), values1, align='center', fc='#80d0f1', ec='w')
ax1.set_yticks(range(n1))
ax1.set_yticklabels(['' if e == 0 else Szconv(e,unit) for e in values1], color='gray')#this need more work to put the GB or the TB
ax1.tick_params(pad=10)
for i, (label, val) in enumerate(Dict1):
ax1.annotate(label.title(), xy=(10, i), fontsize=10, va='center')
for spine in ('top', 'right', 'bottom', 'left'):
ax1.spines[spine].set_visible(False)
ax1.xaxis.set_ticks([])
ax1.yaxis.set_tick_params(length=0)
ax1.invert_yaxis()
ax1.set_title('PrevMonth')
#---------------------------------------------------------------------
#fig.set_size_inches(4, 3.5)
plt.savefig("BarChart/"+ImgName,bbox_inches='tight')
plt.show()
Testdic =OrderedDict([('Exploit', 14664), ('Botnet', 123), ('Virus', 52)])
Testdic2 =OrderedDict([('Botnet', 1252), ('Virus', 600), ('XSS', 452)])
imgname="TestImageformt.png"
unit = "transaction"
chartmak(Testdic,Testdic2,imgname,unit)
您可以根据您的条件更改
Rectangle
所创建的Rectangle
面片的颜色。我对你的代码做了一点修改,去掉了一些不必要的部分,让它运行起来。我希望它清楚地表明它在做什么。例如,可以使用
ax.patch[i].set_color('r')
将补丁i
的颜色更改为红色。相关问题 更多 >
编程相关推荐