I have a duckdb database at ../weather-4.duckdb with the following fields:
station, report_date (timestamp), hour (int), sun_loc_x.
The table name is weather.
For station = ‘station_d856629c9d214e17a3d344e6166b6d08’ , I would like to have a 3d plot for the location of the sun as a function of the hour and date.
How would I do that?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I can help you create a 3D plot of the sun's location using Python with matplotlib or plotly. Here's how you can do it using both libraries:
Using Plotly (interactive 3D plot):
python
import duckdb
import plotly.express as px
import pandas as pd
# Connect to the database
conn = duckdb.connect('../weather-4.duckdb')
# Query the data
query = """
SELECT report_date, hour, sun_loc_x
FROM weather
WHERE station = 'station_d856629c9d214e17a3d344e6166b6d08'
ORDER BY report_date, hour
"""
# Execute query and convert to pandas DataFrame
df = conn.execute(query).df()
# Create 3D scatter plot
fig = px.scatter_3d(df,
x=df.report_date,
y=df.hour,
z=df.sun_loc_x,
title="Sun Location Over Time")
fig.update_layout(
scene = dict(
xaxis_title='Date',
yaxis_title='Hour',
zaxis_title='Sun Location X'
)
)
fig.show()
Using Matplotlib (static 3D plot):
python
import duckdb
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Connect to the database
conn = duckdb.connect('../weather-4.duckdb')
# Query the data
query = """
SELECT report_date, hour, sun_loc_x
FROM weather
WHERE station = 'station_d856629c9d214e17a3d344e6166b6d08'
ORDER BY report_date, hour
"""
# Execute query and convert to pandas DataFrame
df = conn.execute(query).df()
# Create figure and 3D axes
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
# Convert dates to numbers for plotting
dates_num = pd.to_numeric(df.report_date)
# Create the scatter plot
scatter = ax.scatter(dates_num,
df.hour,
df.sun_loc_x,
c=df.sun_loc_x, # Color by sun location
cmap='viridis')
# Add labels and title
ax.set_xlabel('Date')
ax.set_ylabel('Hour')
ax.set_zlabel('Sun Location X')
plt.title('Sun Location Over Time')
# Add colorbar
plt.colorbar(scatter)
# Rotate the plot for better view
ax.view_init(elev=20, azim=45)
plt.show()
Both scripts will:
Connect to your DuckDB database
Query the data for the specified station
Create a 3D plot where:
X-axis is the date
Y-axis is the hour
Z-axis is the sun_loc_x value
The Plotly version gives you an interactive plot that you can rotate and zoom, while the Matplotlib version creates a static plot.
Choose the one that better suits your needs. The Plotly version is generally better for interactive exploration, while the Matplotlib version might be better for publications or static presentations.
Note: You might need to install additional packages:
bash
pip install plotly # for the Plotly version
pip install matplotlib pandas # for the Matplotlib version
You can adjust the visual aspects (colors, angles, labels, etc.) by modifying the respective parameters in either version.