## How to add trendline to a scatter plot

as explained here With help from numpy one can calculate for example a linear fitting. # plot the data itself pylab.plot(x,y,’o’) # calc the trendline z = numpy.polyfit(x, y, 1) p = numpy.poly1d(z) pylab.plot(x,p(x),”r–“) # the line equation: print “y=%.6fx+(%.6f)”%(z,z)

## How to create a density plot

Five years later, when I Google “how to create a kernel density plot using python”, this thread still shows up at the top! Today, a much easier way to do this is to use seaborn, a package that provides many convenient plotting functions and good style management. import numpy as np import seaborn as sns … Read more

## Save plot to numpy array

This is a handy trick for unit tests and the like, when you need to do a pixel-to-pixel comparison with a saved plot. One way is to use fig.canvas.tostring_rgb and then numpy.fromstring with the approriate dtype. There are other ways as well, but this is the one I tend to use. E.g. import matplotlib.pyplot as … Read more

## How to smooth a curve for a dataset

I prefer a Savitzky-Golay filter. It uses least squares to regress a small window of your data onto a polynomial, then uses the polynomial to estimate the point in the center of the window. Finally the window is shifted forward by one data point and the process repeats. This continues until every point has been … Read more

## How is numpy’s fancy indexing implemented?

You have three questions: 1. Which __xx__ method has numpy overridden/defined to handle fancy indexing? The indexing operator [] is overridable using __getitem__, __setitem__, and __delitem__. It can be fun to write a simple subclass that offers some introspection: >>> class VerboseList(list): … def __getitem__(self, key): … print(key) … return super().__getitem__(key) … Let’s make an … Read more

## module ‘pandas’ has no attribute ‘read_csv

Try renaming your csv.py to something else, like csv_test.py. Looks like pandas is being confused about what to import.

## Is there a standard solution for Gauss elimination in Python?

I finally found, that it can be done using LU decomposition. Here the U matrix represents the reduced form of the linear system. from numpy import array from scipy.linalg import lu a = array([[2.,4.,4.,4.],[1.,2.,3.,3.],[1.,2.,2.,2.],[1.,4.,3.,4.]]) pl, u = lu(a, permute_l=True) Then u reads array([[ 2., 4., 4., 4.], [ 0., 2., 1., 2.], [ 0., 0., … Read more

## numpy with python: convert 3d array to 2d

You need to use np.transpose to rearrange dimensions. Now, n x m x 3 is to be converted to 3 x (n*m), so send the last axis to the front and shift right the order of the remaining axes (0,1). Finally , reshape to have 3 rows. Thus, the implementation would be – img.transpose(2,0,1).reshape(3,-1) Sample … Read more

## Parse a Pandas column to Datetime when importing table from SQL database and filtering rows by date

Pandas is aware of the object datetime but when you use some of the import functions it is taken as a string. So what you need to do is make sure the column is set as the datetime type not as a string. Then you can make your query. df[‘date’] = pd.to_datetime(df[‘date’]) df_masked = df[(df[‘date’] … Read more

## Plotting implicit equations in 3d

You can trick matplotlib into plotting implicit equations in 3D. Just make a one-level contour plot of the equation for each z value within the desired limits. You can repeat the process along the y and z axes as well for a more solid-looking shape. from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy … Read more