0

Now there are two list value as following:

lst1 = [17, 24, 16, 3, 9, 10, 8, 7, 6]
lst2 = [25, 30, 29, 6, 7, 10, 1, 2, 8]

and x labels:

lst_p =  ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9']

I want to plot the bars with lst1 and lst2 with different color based on the same x labels.

The code I tried:

width = 0.5
plt.hist((lst1, lst2),label = ("data1", "data2")) # A bar chart
plt.legend()
plt.xticks(lst_p)

but there is error:

AttributeError: 'NoneType' object has no attribute 'seq'

gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41
tktktk0711
  • 1,656
  • 7
  • 32
  • 59
  • it is different and there is error use the same code: TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype(' – tktktk0711 Jan 28 '19 at 09:11
  • 1
    How is it different? The first answer to the question shows you how to generate two histograms in one plot -- isn't this what you want? – Thomas Kühn Jan 28 '19 at 09:31
  • I think it is different. The x labels in my question a str list, not numbers. also the Y is value of two list values. – tktktk0711 Jan 28 '19 at 09:46
  • @ThomasKühn: The OP wants a barchart instead of a Histogram. There is a duplicate somewhere. The duplicate I marked is I think exactly what OP wants – Sheldore Jan 28 '19 at 10:21
  • @Bazingaa Oh, I see. I got confused by the use of `plt.hist` then. How should I best handle this? If I retract my closing vote, I cant re-vote for this to be a duplicate... – Thomas Kühn Jan 28 '19 at 10:25
  • @ThomasKühn: It's ok, the OP can decide which dupe answers the problem best. No issues :) – Sheldore Jan 28 '19 at 10:28

1 Answers1

1

As stated in the xticks() documentation the firsts argument are the tick locations. The labels are only the second argument. So you have to pass also the xtick locations to the function call and it works

plt.xticks(plt.gca().get_xaxis().get_ticklocs(), lst_p)

enter image description here

gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41