-
-
Save cmrivers/33b1d00d00eefcbf735e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#create string variable for Year | |
r['new_year'] = r.Year.astype('str') | |
#set colors for different plot type | |
colors1=sns.color_palette('husl', 8) #number represents how many things being plotted | |
#create plot in a different way so can enable markers specific to each MACOM | |
#can mess with the height and width using the figsize= | |
fig, ax = plt.subplots(figsize=(8, 4)) | |
#call in each variable to be plotted and specify the marker to use | |
#had to google what markers are available in matplotlib | |
##### hmm, changed my mind...try reverting to original year | |
ax.plot(r.original_year, r.ARMY, marker='o', linewidth=2,label='Army', color=colors1[0]) | |
ax.plot(r.original_year, r.MEDCOM, marker='v', linewidth=2,label='MEDCOM', color=colors1[1]) | |
ax.plot(r.original_year, r.TRADOC, marker='H', linewidth=2,label='TRADOC', color=colors1[2]) | |
ax.plot(r.original_year, r.USAREUR, marker='^', linewidth=2,label='USAREUR', color=colors1[3]) | |
ax.plot(r.original_year, r.FORSCOM, marker='8', linewidth=2,label='FORSCOM', color=colors1[4]) | |
ax.plot(r.original_year, r.USARPAC, marker='s', linewidth=2,label='USARPAC', color=colors1[5]) | |
ax.plot(r.original_year, r.USASOC, marker='s', linewidth=2,label='USASOC', color=colors1[6]) | |
ax.plot(r.original_year, r.EUSA, marker='D', linewidth=2,label='EUSA', color=colors1[7]) | |
ax.legend() | |
#add commas to y axis | |
ax.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ','))) | |
#####relabel x axis tickes to match string years | |
ax.set_xticklabels(['2001', '2002', '2003']) #put the labels you want in here | |
#####if you have a lot of labels, you can generate the range programatically like this: | |
new_labels = [str(i) for i in range(1980, 2006)] # but then again that would probably be too crowded | |
ax.set_xticklabels(new_labels) | |
#label axes, move legend to outside plot, despine | |
ax.set_ylabel('Injury Rate per 1,000 Person Years') | |
ax.set_xlabel('Year') | |
ax.legend(bbox_to_anchor=(1.33, .9)) | |
sns.despine() | |
##save as a figure | |
fig.savefig('Figure1_Marker.png') | |
#for some reason the whole plot isn't being saved? | |
####is the saved figure just off center or something like that? | |
####Sometimes if you have a weird sized or large figure you can try adding bbox='tight' and see if that helps. | |
####Your figure is a pretty normal size though, so I don't know why it would be fussy. | |
''' | |
another idea - when you're iterating through something, like when you choose a different color for each line of your plot, | |
you can use itertools. Here's what it would look like: | |
from itertools import cycle | |
colors1 = cycle(sns.color_palette('husl', 8)) | |
ax.plot(x, y, color=next(colors1)) | |
ax.plot(x, y2, color=next(colors1)) | |
so basically you 1) import itertools 2) put cycle() around your list 3) use next() to call the next item | |
The way you are currently doing it is totally fine though, it's just one less thing to keep track of. | |
One final thing you might find useful if you're plotting several lines is the alpha argument, which turns down the opacity. | |
Just add e.g. alpha=.5 inside your ax.plot() call. | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment