Hello Folks! I want to generate graph from data scrapped from website which is in CSV.
My requirement is either static / interactive graph which can be later sent to Telegram message PNG if static.
I have tried using code node in n8n in python but it didn’t work.
import matplotlib.pyplot as plt
import pandas as pd
# Load your CSV data
df = pd.read_csv('data.csv')
# Plot the data
plt.plot(df['column1'], df['column2'])
# Save as PNG
plt.savefig('graph.png')
@Rehare have you tried using a plotting library like matplotlib in a code node.
You can use the code above as a starting point.
Thanks for the reply. How do I ensure matplotlib and pandas are available in n8n’s code node environment and
- n8n’s code node runs in a Node.js environment, not Python
- Does this require setting up a separate Python service?
Yes n8n’s Code node uses Node.js which means you can’t directly use Python libraries like Matplotlib. There’s a way to get around it.
You set up a separate Python service that generates the graph and exposes an API for n8n to call. Or, use Node.js libraries like Plotly.js or Chart.js to generate the graph directly in the Code node.
If you’re more comfortable with Python, setting up a separate service might be the way to go. We can use Flask or FastAPI to create a simple API that generates the graph and returns the image. Then, in n8n, we can use the HTTP Request node to call that API and get the graph.
@Rehare Instead of trying to run Python use a JavaScript charting library in n8n’s native code node:
// Install: npm install chart.js node-canvas
const { createCanvas } = require('canvas');
const Chart = require('chart.js/auto');
const data = $input.all(); // Get data from previous nodes
const canvas = createCanvas(800, 600);
const ctx = canvas.getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: data.map(item => item.json.xValue),
datasets: [{
label: 'My Data',
data: data.map(item => item.json.yValue)
}]
}
});
// Return the image for next nodes
return [{
json: { chartGenerated: true },
binary: {
chartImage: {
data: canvas.toBuffer('image/png'),
mimeType: 'image/png'
}
}
}];
How do I pass data from previous nodes into this Python code?
Ok better now thanks
I’ll do this and give feedback tomorrow