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
... ... ... ... ... ...