Data:
import pandas as pd
data = {'PctUrban': [1.460065484046936, 1.460065484046936, 1.460065484046936, 1.460065484046936, 1.460065484046936, 1.460065484046936, 1.460065484046936, 1.460065484046936, 1.460065484046936, 1.460065484046936],
'huc6': [30201, 30201, 30201, 30201, 30201, 30201, 30201, 30201, 30201, 30201], 'contam_gro': ['Butyltins', 'Chlordanes', 'Chlorobenzenes', 'DDTs', 'Dieldrins', 'Endolsulfans', 'HCHs', 'Other', 'PAHs', 'PBBs'],
'matrix': ['SED', 'SED', 'SED', 'SED', 'SED', 'SED', 'SED', 'SED', 'SED', 'SED'], 'sample_typ': ['sediment', 'sediment', 'sediment', 'sediment', 'sediment', 'sediment', 'sediment', 'sediment', 'sediment', 'sediment'],
'HUC_tot_conc_avg': [0.5375, 0.1654999999999999, 0.488499999999625, 0.373, 0.2996249999995875, 0.0075, 0.005, 0.0032083333333375, 81.17823809514286, 0.0],
'region': ['South Atlantic', 'South Atlantic', 'South Atlantic', 'South Atlantic', 'South Atlantic', 'South Atlantic', 'South Atlantic', 'South Atlantic', 'South Atlantic', 'South Atlantic']}
Sediment = pd.DataFrame(data, index=range(0, 19, 2))
I am creating a series of plot in a loop. Contam_gro column has 13 unique values - the loop grabs a unique value and creates a scatterplot. I need to add a trendline(or best fitting line) to each plot.
The code for a loop to create the scatter plots:
for contam in Contam_list:
Sediment.loc[(Sediment['contam_gro'] == contam)].plot(x='PctUrban', y='HUC_tot_conc_avg', kind='scatter', figsize=(7,4))
plt.title(contam, fontsize=18) #Labeling titel
plt.xlabel('Urban %', fontsize=12) #Labeling x-axis
plt.ylabel('Concentration', fontsize=12) #Labeling y-axis
plt.show()
I have to use matplotlib for this. I am not sure how and where to implement the trendline code into the loop.
I tried adding this piece:
Sediment.loc[(Sediment['contam_gro'] == contam)].plot(Sediment['PctUrban'],p(Sediment['PctUrban']),"r--", x='PctUrban', y='HUC_tot_conc_avg', kind='scatter', figsize=(7,4),
z = np.polyfit(Sediment['PctUrban'], Sediment['HUC_tot_conc_avg'], 1), p = np.poly1d(z))
Error is "dict() got multiple values for keyword argument 'x'"