Matplotlib - 为什么条形图的线条颜色是黑色?

5

我正在使用Matplotlib绘制浮点数列表的图表。如果我的列表长度为100个浮点数,图表会显示正确的颜色。但是如果列表长度为785个浮点数,则只会显示黑色。以下是代码。

import numpy as np
import matplotlib.pyplot as plt
import Image

Consensus = []
Prediction = []
Final = []
for line in open('1.out').readlines():
    words = line.split()
    Consensus.append(float(words[10]))
    Prediction.append(int(words[11]))

for i,j in zip(Consensus,Prediction):
    Final.append(i*j)

max_score = 3
length = 785
ind = np.arange(length)    # the x locations for the groups
width = 1       # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, Consensus, width, color='red')
p2 = plt.bar(ind, Final, width, color='blue')

plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(np.arange(0,length,50))
plt.yticks(np.arange(0,max_score,0.2))
plt.savefig('testplot.png')
Image.open('testplot.png').save('testplot.jpg','JPEG')

这是当列表长度为785时程序的图片。这是当列表长度为785时程序的图片。 这是列表长度为99时的情况。这是列表长度为99时的情况。 文件可以在这里下载 - http://pastebin.com/HhPhgPDG 你可以复制该文件的前100行来检查其他情况。应将length变量更改为文件中的行数。
谢谢。
2个回答

26

你的条形图边框是黑色的。当你有很多条时,它们可能变得非常窄,边框会混在一起,所以你只能看到边框而看不到有色内部。尝试缩放图表以查看颜色是否存在。你也可以向bar传递一个edgecolor='none'参数来去除边框。


1

你的条形图变得如此密集,以至于只剩下边框可见。由于它们是黑色的,你主要看到的是黑色。有两种可能的解决方法:

a)增加条形的宽度

b)去掉条形的边框


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接