Refining a visual often involves getting rid of unnecessary elements and directing a viewer's attention to specific elements. The process to enhance clearity, readability, and effectiveness is a iterative process that requires constant self-evaluations. This process requires many hours, although the output may look simple.

Can Large Language Models (LLMs) be used as a tool for generating visuals? While LLMs excel at text-based tasks, their ability to understand and generate complex concepts can be leveraged to assist in writing code to create visuals. But can LLMs be used to refine visuals? This post tests LLM's ability to refine Plotly visuals.

1. Asking Gemini to create a Plotly Chart

Gemini Logo

🔮 Prompt

Generate a Plotly Python code to compare the stock prices of Adidas and Nike for the past two years.

❇️ Gemini Response

# Python
import yfinance as yf
import plotly.graph_objects as go

# Get stock data for Adidas and Nike
adidas = yf.download('ADDYY', start='2022-09-28', end='2024-09-28')
nike = yf.download('NKE', start='2022-09-28', end='2024-09-28')

# Create a figure
fig = go.Figure()

# Add traces for Adidas and Nike
fig.add_trace(go.Scatter(x=adidas.index, y=adidas['Close'], name='Adidas'))
fig.add_trace(go.Scatter(x=nike.index, y=nike['Close'], name='Nike'))

# Update layout
fig.update_layout(
    title='Adidas vs. Nike Stock Price Comparison',
    xaxis_title='Date',
    yaxis_title='Stock Price'
)

# Show the plot
fig.show()

Explanation:

  1. Import necessary libraries: yfinance for downloading stock data and plotly.graph_objects for creating the plot.
  2. Get stock data: Use yf.download() to fetch the closing prices of Adidas (ADDYY) and Nike (NKE) from September 28, 2022, to September 28, 2024.
  3. Create a figure: Initialize a go.Figure object to hold the plot.
  4. Add traces: Add two scatter plots to the figure, one for Adidas and one for Nike. The x values are the dates, and the y values are the closing prices.
  5. Update layout: Customize the plot's title, x-axis label, and y-axis label.
  6. Show the plot: Display the created plot.

Note: Replace '2022-09-28' and '2024-09-28' with the desired start and end dates if you want to analyze a different time period.

This code will generate an interactive line chart comparing the stock prices of Adidas and Nike over the specified time frame. You can customize the plot further by adding annotations, changing colors, or adjusting other layout properties.

👇 Run the code without any modifications.

In [1]:
import yfinance as yf
import plotly.graph_objects as go

# Get stock data for Adidas and Nike
adidas = yf.download('ADDYY', start='2022-09-28', end='2024-09-28')
nike = yf.download('NKE', start='2022-09-28', end='2024-09-28')

# Create a figure
fig = go.Figure()

# Add traces for Adidas and Nike
fig.add_trace(go.Scatter(x=adidas.index, y=adidas['Close'], name='Adidas'))
fig.add_trace(go.Scatter(x=nike.index, y=nike['Close'], name='Nike'))

# Update layout
fig.update_layout(
    title='Adidas vs. Nike Stock Price Comparison',
    xaxis_title='Date',
    yaxis_title='Stock Price'
)

# Show the plot
fig.show()
[*********************100%***********************]  1 of 1 completed
[*********************100%***********************]  1 of 1 completed