Skip to content

notY0rick/matplotlib_ai

Repository files navigation

matplotlib_ai

Do you also have a love-hate relationship with matplotlib? So do I! That's why I created this mini-project that can help you graph your data using natural language. The package dependencies require openai and matplotlib, and it is unbelievably easy to use. Calling OpenAI's GPT API, prompt engineering, and using few-shot learning, matplotlib_ai is capable of generating graphs without requiring you to write a single line of matplotlib code!

Import matplotlib_ai via pip:

pip install matplotlib_ai

Say we have a dictionary data with 4 curves labeled 'a', 'b', 'c', and 'd':

import numpy as np
data = {'a': [...], # some curve
        'b': [...], # some curve
	'c': [...], # some curve
	'd': [...], # some curve}

If we wanted to graph each curve and make curve 'a' dashed and call this graph "my ekg when i see you :)", the most sensible thing would be to write matplotlib code as such:

import matplotlib.pyplot as plt
plt.plot(data['a'], linestyle='dashed', label='a')
plt.plot(data['b'], label='b')
plt.plot(data['c'], label='c')
plt.plot(data['d'], label='d')
plt.title('my ekg when i see you :)')
plt.legend()
plt.show()

However, with matplotlib_ai it is as easy as:

from matplotlib_ai.matplotlib_ai import matplotlib_ai
mpl_ai = matplotlib_ai("YOUR-OPENAI-API-KEY")
prompt = "graph a curve for each item in data and title the graph 'my ekg when i see you :)'. " + 
	 "Make curve 'a' in data a dashed line."
code = mpl_ai(prompt)

Then, mpl_ai would generate:

yuhhhh

To see the code generated by GPT, simply print it like so:

>>> print(code)		# the code generated by GPT
import matplotlib.pyplot as plt
for key, value in data.items():
    if key == 'a':
        plt.plot(value, linestyle='dashed', label=key)
    else:
        plt.plot(value, label=key)
plt.title('my ekg when i see you :)')
plt.legend()
plt.show()

This project is at its early stages, I hope to make it more comprehensive in time :)