![]() I like using seaborn to make small multiple plots, but it also has a very nice 2d kernel density contour plot method I am showing off. The further examples I show are using the seaborn library, imported earlier as sns. It is maybe easier to make interactive graphs and have a tooltip, but that will need to be another blog post as well. You can add in text by using ax.annotate fairly easy by hand, but it is hard to automate text label positions. I debated on making some notes here about putting in text labels, but will hold off for now. It is also a good use case for funnel charts. This could technically be correct (only some local one cop town reported, and that town does not cover the whole county), but I’m wondering if this is a UCR reporting snafu. You may be asking, what is up with that outlier in the top right? It ends up being Letcher county in Kentucky in 1983, which had a UCR population estimate of only 1522, but had a total of 136 burglaries and 7 robberies. (Using white for the edge colors looks nice as well.) So for the hexbins I like using the mincnt=1 option, as it clearly shows areas with no points, but then you can still spot the outliers fairly easy. Plt.savefig('Scatter04.png', dpi=500, bbox_inches='tight') Matplotlib has a very nice hexbin method for doing this, which is easier to show than explain. One way to solve that problem is to bin observations, and then make a graph showing the counts within the bins. For most datasets of anymore than a hundred points, you often have severe overplotting like you do here. Plt.savefig('Scatter03.png', dpi=500, bbox_inches='tight') #Making the points small and semi-transparentĪx.scatter(crime_dat, crime_dat, c='k', Note I don’t set the edgecolor here, but if you want to make the edges semitransparent as well you could do edgecolor=(0.0, 0.0, 0.0, 0.5), where the last number of is the alpha transparency tuner. This allows you to further visualize the density, but then makes it a bit harder to see individual points – if you started from here you might miss that outlier in the upper right. Another quick trick is to make the points smaller and up the transparency by setting alpha to a lower value. ![]() So that is better, but we still have quite a bit of overplotting going on. Plt.savefig('Scatter02.png', dpi=500, bbox_inches='tight') ![]() #Making points have an outline and interior fillĪx.scatter(crime_dat, crime_dat, All of this action is going on in the ax.scatter call, all of the other lines are the same. Here I also make the interior fill slightly transparent. I think a better default for scatterplots is to plot points with an outline. You can see here the default point markers, just solid blue filled circles with no outline, when you get a very dense scatterplot just looks like a solid blob. Plt.savefig('Scatter01.png', dpi=500, bbox_inches='tight') Then I set the axis grid lines to be below my points (is there a way to set this as a default?), and then I set my X and Y axis labels to be nicer than the default names. I don’t have a good reason for using one or the other. You could also instead of starting from the matplotlib objects start from the pandas dataframe methods (as I did in my prior histogram post). After defining my figure and axis objects, I add on the ax.scatter by pointing the x and y’s to my pandas dataframe columns, here Burglary and Robbery rates per 100k. ![]() My_dir = r'C:\Users\andre\OneDrive\Desktop\big_scatter'Ĭrime_dat = pd.read_csv('Rural_appcrime_long.csv')įirst, lets start from the base scatterplot. I technically do not use numpy in this script, but soon as I take it out I’m guaranteed to need to use np. ![]() So first for the upfront junk, I load my libraries, change my directory, update my plot theme, and then load my data into a dataframe crime_dat. Here you can download the dataset and the python script to follow along. customizing a template, adding legends, etc.)įor this post, I am going to use the same data I illustrated with SPSS previously, a set of crime rates in Appalachian counties.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |