0

I'm working with some data on all previous US Senators. I want to graph the frequencies of various tenure lengths, separated by party for the Democrats, Republicans, Whigs, Federalists, and Democratic-Republicans. I've gotten the plot made but having it give me information for every year is really hard to read, so I want to downsample by terms of 6 years. If this were a simple histogram, I would just reduce the number of bins, but since it's a bar plot I get AttributeError: Unknown property bins when I try to specify bins as a parameter.
I want something like this but less finely grained:

def tenure_count(frame, party):
    return frame[frame.party == party].tenure.value_counts().reindex(
        [x for x in range(0, 52) if x % 1 == 0], fill_value=0)


df = pd.read_csv("congress_tenure.csv", header=0)
df = df[['tenure', 'party']].sort_values('tenure')

parties = ['Democrat', 'Republican', 'Democratic-Republican', 'Whig', 'Federalist']
counts = pd.DataFrame(
    np.column_stack(
        [tenure_count(df, x) for x in parties]
    )
)
counts.columns = parties
counts.index.name = 'tenure'

counts.plot(kind='bar')
plt.show()

The counts DataFrame look like:

        Democrat  Republican  Democratic-Republican  Whig  Federalist
tenure                                                               
0             63          25                      7     0           6
1             82          59                     19     9           8
2             52          21                     12     3          11
3             36          17                     15     3          14
4             40          20                     11     6           9
5             39          37                     10     4           5
...           ...         ...                    ...    ...         ...
embradley
  • 475
  • 1
  • 5
  • 14
  • Can you give a sample of what `counts` looks like? Looks like you are actually using a historam though. So you can set parameters like `counts.plot(kind='bar', bins=bins)`. – busybear Mar 29 '19 at 23:38
  • Trying to set the `bins` raises `AttributeError: Unknown property bins` – embradley Mar 29 '19 at 23:42
  • Try `counts.hist(bins=bins)`. I think when you use `kind` it has to be formatted differently. – busybear Mar 29 '19 at 23:44
  • That gives a completely different graph. What I want is a less finely grained version of [this](https://imgur.com/yx3NyzH), but using `hist` gives [this](https://imgur.com/zwxQC5e) – embradley Mar 29 '19 at 23:49

1 Answers1

1

You can bin your data using the approach here. You just need to decide what you want to do with the values: sum, average, etc. It would look something like this:

binned = counts.groupby(counts.index // 6).sum()
binned.plot.bar()
busybear
  • 10,194
  • 1
  • 25
  • 42