Python Forum
How can histogram bins be separated and reduce number of labels printed on x-axis?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can histogram bins be separated and reduce number of labels printed on x-axis?
#1
I have a series of Counter() whose graphs I want to plot. The issue is that there are around 120 to 150 bars that are plotted. I've tried playing with the width but it didn't scale at all for me and all the x-axis labels were mashed together. Therefore, I decided to print instead a graph with only 50 bars but still all the x-axis labels are being printed. I've tried things like labels[:50] but it doesn't work at all and I tried implementing the solution from another post to space out the bars but it was for naught (matplotlib bar chart: space out bars).

Therefore, could someone tell me how can I:

1. If I want to print all 120 to 150 bars how can the graph be properly scaled, if possible?

2. How can I print 50 bars x-axis labels instead of all the labels in my data set.

3. How can I space out the bars when dealing with Counter()?

The code for my plot function and the screenshots of the graphs are:

def plot_count(mycount):

    labels, values = zip(*mycount.items())
    indexes = np.arange(len(labels))
    width = 1
    plt.bar(indexes[:50], values[:50], width)
    plt.xticks(indexes + width * 0.5, labels, rotation = 45)
    plt.show()
All bars graph
   

5 bars graph
   
Reply
#2
This may help: https://stackoverflow.com/a/56021895
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  coma separator is printed on a new line for some reason tester_V 4 726 Feb-02-2024, 06:06 PM
Last Post: tester_V
  reduce nested for-loops Phaze90 11 2,372 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Adding values with reduce() function from the list of tuples kinimod 10 3,185 Jan-24-2023, 08:22 AM
Last Post: perfringo
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,305 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Unhashable error - Histogram code lsbpython 1 1,111 Aug-07-2022, 04:02 PM
Last Post: Yoriz
  How to avoid the extra set of y-axis labels? Mark17 9 2,859 May-17-2022, 06:26 PM
Last Post: Mark17
  Floor division problem with plotting x-axis tick labels Mark17 5 2,399 Apr-03-2022, 01:48 PM
Last Post: Mark17
  x-axis labels with Matplotlib Mark17 8 2,584 Mar-23-2022, 06:10 PM
Last Post: Mark17
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,814 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  How do I reduce the time to Invoke Macro via Python? JaneTan 1 2,302 Dec-28-2020, 06:46 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020