1

I am trying to plot a single bar plot with multiple colors...

 import numpy as np
 width = 0.3   
 y1 =[78.0]
 y2= [70863.0]
 y3 =[138441.0]
 y4 =[ 8223.0]

x = np.range(1)

p1 = plt.bar(x, y1, width=width, color='r')
p2 = plt.bar(x, y2, width=width, color='b')
p3 = plt.bar(x, y3, width= width, color='g')
p4 = plt.bar(x, y4, width= width, color='y')

plt.show()

enter image description here

1) It makes a very ugly plot. How do i control the width of my plot

2) The plot is not right.. y2 is missing

3
  • Are you sure your code is correct? numpy doesn't have a range() function, although it does have an arange() function. What is ind? Why is the bar plot being made for a one-object list and a range? Also, to answer 1: plt.xlim(xmin,xmax). Commented Mar 5, 2015 at 19:05
  • what is ind in your code? Commented Mar 5, 2015 at 19:05
  • @James: sorry it was x instead of ind Commented Mar 5, 2015 at 21:46

2 Answers 2

3

I'm operating on the assumption that your code is not exactly the same as you entered it, as, for example, np.range(1) returns an error and you have an unclosed string on your y4 plot command. Because, making some small edits, I can reproduce your plot, except my colors are different than yours. If p3's color is 'b' and p4's is 'g', then you can make some simple fixes.

Preventing hidden bars

When you plot a bar at x = 0, it will arrange itself at the front of your plot. If that means it covers up something, then your old data will be covered up. In this case, you are plotting y3 over top y2. There are two ways to fix this:

  1. Use the zorder kwarg to set the order in which things appear. Things with, e.g., zorder=5 appear on top of things with zorder=4. See the documentation on the defaults for zorder. In this particular instance, you would want
    p1 = plt.bar(x, y1, width=width, color='r',zorder=4) 
    p2 = plt.bar(x, y2, width=width, color='b',zorder=2)
    p3 = plt.bar(x, y3, width= width, color='g',zorder=1)
    p4 = plt.bar(x, y4, width= width, color='y',zorder=3)
  1. Plot them in order, from largest to shortest. If your plot commands are p3, p2, p4, then p1, then they will be drawn on overtop of each other without covering any of them up.

  2. Space them differently. As you currently plot them, you are having them all based at the same point. If you vary the x coordinate, such as x1,x2,x3,x4 = range(4) and then change your plot command to, e.g. p1 = plt.bar(x1, y1, width=width, color='r') then you won't have missing bars.

Controlling the width (and height)

Matplotlib controls the limits of the plot with plt.xlim() and plt.ylim(). To set a plot's x-axis to go from, say, 0 to 4, you use plt.xlim(0,4). Likewise, if you want to add some white space to the top of your plot to improve the appearance, you can use plt.ylim(0, 150000).

If your statement was instead asking how to make different widths for your error bars, then you can use the width= kwarg in plt.bar(). Right now, it's being set to 0.3, since you earlier defined the variable width=0.3. Changing that, whether on a case-by-case basis or by changing the variable itself, will control how wide the bars are.

Two other comments

  1. If you are just plotting a bar that is one point at one point, you don't actually need them to be lists. You can just use plt.bar(2,4) to produce a bar at x=2 that is 4 units high (This also shows that you don't need to set p1 = plt.bar...)

  2. If you are displaying data that span so many orders of magnitude, it might be helpful to set the y-scale to be logarithmic. This can be done with plt.yscale('log',noposy=clip). That latter kwarg is useful in case you have a y value of 0 -- it will just clip it from the plot instead of breaking.

Sign up to request clarification or add additional context in comments.

Comments

1

One easy way to plot the levels with number of occurrence of each levels in the particular column : This may be useful in ur case. Here the output column in the data(DF) have two levels ( 0 ) & (1 ) and Y axis shows the count of each levels.

# Target varible bar chart
def snsplot(t):
    sns.countplot(x=t,data=data,palette='hls')
    print('The target variable',t, 'as shown as bar chart')
    plt.show()
#plt.savefig('count_plot')

snsplot(Target_col_Y)

enter image description here

1 Comment

Please fix the formatting of your answer, and/or stop rescinding the revision that has been suggested multiple times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.