Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib - why is Bar graph line color is black?

I am using Matplotlib to graph list of floats. If my list is 100 floats long the graph displays the proper colors. But if the list is 785 floats long then the it only shows black color. Here is the code.

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')

This is the picture of the program when the list is 785 in length. This is the picture of the program when the list is 785 in length.

This is when the the list is 99 in length. This is when the the list is 99 in length.

The file is available here - http://pastebin.com/HhPhgPDG

You can change just copy the first 100 lines of this file to check for the other case. You should change the length variable to the number of lines in the file.

Thanks.

like image 911
fatrock92 Avatar asked Sep 11 '12 18:09

fatrock92


People also ask

How do I change the color of a bar graph in matplotlib?

You can change the color of bars in a barplot using color argument. RGB is a way of making colors. You have to to provide an amount of red, green, blue, and the transparency value to the color argument and it returns a color.

How do I change the line color in matplotlib?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument.

What are the default colors in matplotlib?

MatPlotLib with Python The default color of a scatter point is blue. To get the default blue color of matplotlib scatter point, we can annotate them using annotate() method.

How do you change the color of a plotted line?

plot method type the points or lines that you want to plot. In the brackets, if you don't mention the line color as shown above; by default, it chooses the blue color. You can change the color by using full names color=”red”. You can change the color by using hex strings ('#008000').


1 Answers

The borders of your bars are black. It's possible that when you have so many bars, they become very narrow and the borders mush together, so you only see the borders and not the colored insides. Try zooming in on your graph to see if the colors are there. You could also pass an edgecolor='none' argument to bar to remove the borders.

like image 99
BrenBarn Avatar answered Oct 28 '22 02:10

BrenBarn