【问题标题】:Matplotlib - why is Bar graph line color is black?Matplotlib - 为什么条形图线颜色是黑色的?
【发布时间】:2012-09-04 17:22:26
【问题描述】:

我正在使用 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 时的程序图片。

此时列表的长度为 99。

该文件在此处可用 - http://pastebin.com/HhPhgPDG

您可以更改仅复制此文件的前 100 行以检查其他情况。您应该将长度变量更改为文件中的行数。

谢谢。

【问题讨论】:

    标签: python colors matplotlib bar-chart


    【解决方案1】:

    您的条形边框是黑色的。有可能当你有这么多条时,它们会变得很窄并且边界会混合在一起,所以你只能看到边界而不是彩色的内部。尝试放大图表以查看颜色是否存在。您还可以将 edgecolor='none' 参数传递给 bar 以删除边框。

    【讨论】:

      【解决方案2】:

      您的条形变得如此密集,以至于只有边框可见。因为它们是黑色的,所以你看到的主要是黑色。解决此问题的两种可能方法:

      a) 增加条的宽度

      b) 移除条形边框

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-11
        • 1970-01-01
        • 2016-11-15
        • 1970-01-01
        相关资源
        最近更新 更多