Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add % bachelors degrees plot example #4611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 12, 2015
Merged

Conversation

rhiever
Copy link
Contributor

@rhiever rhiever commented Jul 9, 2015

Here's a rework of the "% bachelors degrees conferred to women in the USA" chart that I promised to submit to the gallery ages ago. This doesn't require any custom styles or anything like that - all the styling is right there in the example code.

Here's what the plot looks like with the current code:

percent-bachelors-degrees-women-usa

I still have a couple issues:

  • The title isn't centered over the entire chart. I tried using suptitle, but it looks exactly the same.
  • I'm using a hacky method to generate the grid lines. Suggestions for a better way to do this?

@jenshnielsen
Copy link
Member

Nice. I would replace the figure and axis generation with:

fig, ax = plt.subplots(1, 1, figsize=(12, 14))

I think the title looks badly centred because most of the text is outside the plot. Increasing the xlim to something like 2016 improves the centering for me.

The build error is due to the pep8 (Only whitespace I think)

@tacaswell tacaswell added this to the next point release milestone Jul 10, 2015
@rhiever
Copy link
Contributor Author

rhiever commented Jul 11, 2015

Thanks @jenshnielsen. I made the change you suggested and ran the file through autopep8.

@joferkington
Copy link
Contributor

Great example, @rhiever!

On a side note, you could also write a few parts in a bit more efficient way. Here's a slightly different way of writing it. (I've removed all comments here just to make it fit in a github comment easier. Here's the commented version: https://gist.github.com/joferkington/4eb02c5f1c051772e590)

Regardless, this is just a suggestion, feel free to ignore it.

import matplotlib.pyplot as plt
from matplotlib.mlab import csv2rec
from matplotlib.cbook import get_sample_data

fname = 'percent_bachelors_degrees_women_usa.csv'
gender_degree_data = csv2rec(fname)

color_sequence = ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c',
                  '#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5',
                  '#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f',
                  '#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5']

majors = ['Health Professions', 'Public Administration', 'Education',
          'Psychology', 'Foreign Languages', 'English',
          'Communications\nand Journalism', 'Art and Performance', 'Biology',
          'Agriculture', 'Social Sciences and History', 'Business',
          'Math and Statistics', 'Architecture', 'Physical Sciences',
          'Computer Science', 'Engineering']

y_offsets = {'Foreign Languages': 0.5, 'English': -0.5,
             'Communications\nand Journalism': 2.5,
             'Art and Performance': -0.25, 'Agriculture': 1.25,
             'Social Sciences and History': 0.25, 'Business': -0.75,
             'Math and Statistics': 0.75, 'Architecture': -0.75,
             'Computer Science': 0.75, 'Engineering': -0.25}

fig, ax = plt.subplots(figsize=(12, 14))
plt.setp(ax.spines.values(), visible=False)

ax.grid(axis='y', ls='--', lw=0.5, color='black', alpha=0.3)
ax.set(yticks=range(10, 100, 10), xticks=range(1970, 2020, 10))
ax.tick_params(axis='both', which='both', bottom='off', top='off',
                labelbottom='on', left='off', right='off', labelleft='on',
                labelsize=14)

for color, column in zip(color_sequence, majors):
    column_rec_name = column.replace('\n', '_').replace(' ', '_').lower()
    line, = ax.plot(gender_degree_data.year,
                    gender_degree_data[column_rec_name],
                    lw=2.5, color=color)

    y_pos = gender_degree_data[column_rec_name][-1]
    if column in y_offsets:
        y_pos += y_offsets[column]

    ax.annotate(column, xy=(1, y_pos), xytext=(10, 0), fontsize=14, color=color,
                xycoords=('axes fraction', 'data'),
                textcoords='offset points', va='center')

fig.suptitle('Percentage of Bachelor\'s degrees conferred to women in '
             'the U.S.A. by major (1970-2011)\n', fontsize=18, y=0.95)

ax.margins(x=0, y=0.05)
ax.set_ylim(bottom=0)
fig.subplots_adjust(left=0.05, right=0.75, bottom=0.05)

plt.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight')
plt.show()

@WeatherGod
Copy link
Member

I am going to merge this as-is. Feel free to improve the example in a separate PR.

WeatherGod added a commit that referenced this pull request Jul 12, 2015
Add % bachelors degrees plot example
@WeatherGod WeatherGod merged commit 5795f8d into matplotlib:master Jul 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants