Skip to content

Commit

Permalink
feat (componentization): conversations with Claude
Browse files Browse the repository at this point in the history
  • Loading branch information
santanche committed Sep 28, 2024
1 parent 7ef108b commit bdfc958
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions notebooks/componentization/claude.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Conversas com Claude.ai - Componentization

## Passo 1 - Módulo / Pacote

Consider the following code:


```
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('intake-person-demo(potato-chips).csv')
plt.figure(figsize=(10, 6))
scatter = plt.scatter(df['age'], df['intake_bw'], c=df['intake_bw'], cmap='viridis')
plt.xlabel('age')
plt.ylabel('intake_bw')
plt.title('Age vs. Intake (body weight)')
plt.colorbar(scatter, label='intake_bw')
plt.show()
```

How can I reorganize this code in two functions (table and scatter_plot) and a main code that call them?
* table: receives the path as parameter and reads the CSV, returning the data
* scatter_plot: receives the data, xfield, yfield, and title, ploting the scatter plot
xfield and yfield are both the name of the field in the CSV and its label.

<hr>

### Answer

~~~python

import pandas as pd
import matplotlib.pyplot as plt

def table(path):
"""
Read CSV file and return the data.
:param path: str, path to the CSV file
:return: pandas DataFrame
"""
return pd.read_csv(path)

def scatter_plot(data, xfield, yfield, title):
"""
Create a scatter plot from the given data.
:param data: pandas DataFrame
:param xfield: str, name of the x-axis field
:param yfield: str, name of the y-axis field
:param title: str, title of the plot
"""
plt.figure(figsize=(10, 6))
scatter = plt.scatter(data[xfield], data[yfield], c=data[yfield], cmap='viridis')

plt.xlabel(xfield)
plt.ylabel(yfield)
plt.title(title)

plt.colorbar(scatter, label=yfield)

plt.show()

def main():
# Path to the CSV file
csv_path = 'intake-person-demo(potato-chips).csv'

# Read the data
df = table(csv_path)

# Create the scatter plot
scatter_plot(df, 'age', 'intake_bw', 'Age vs. Intake (body weight)')

if __name__ == "__main__":
main()

~~~

0 comments on commit bdfc958

Please sign in to comment.