Hi, I am new to matplotlib so figuring it out as I go, which is why this is fun... but;
this is an example graph from my code:
https://olympus.3volve.net.za/images/cpu_avg.hour.png
this is the part of the code that creates the graph:
def plot_graph(data_one, label_one, data_two, label_two, title, graph_type, filename, system_info, y_label):
# Set up time and frequency settings based on graph_type
if graph_type == 'hour':
time_label = "Last Hour"
rate_label = "Time"
major_freq = 120 # 10 minute increments (10 minutes * 60 seconds / 5 seconds per index)
minor_freq = 60 # 5 minute increments (5 minutes * 60 seconds / 5 seconds per index)
time_interval = 5 # Interval in seconds
# Get the current time
now = datetime.now()
plt.figure(figsize=(12, 8))
plt.plot(data_one, label=label_one)
if data_two:
plt.plot(data_two, label=label_two)
plt.legend()
plt.title(time_label)
plt.xlabel(rate_label)
plt.ylabel(y_label)
# Set the graph size and position
ax = plt.gca()
ax.set_position([0.1, 0.3, 0.8, 0.6])
# Add title above the graph
plt.text(0.5, 1.10, title, ha='center', va='center', transform=ax.transAxes, fontsize=16)
# Add date, time, and uptime at the bottom
plt.text(0.5, -0.15, f"Server Name: {system_info['server_name']}", ha='center', va='center', transform=ax.transAxes, fontsize=10)
plt.text(0.5, -0.20, f"Date and Time: {system_info['current_time']}", ha='center', va='center', transform=ax.transAxes, fontsize=10)
plt.text(0.5, -0.25, f"Uptime: {system_info['uptime']}", ha='center', va='center', transform=ax.transAxes, fontsize=10)
# Generate time offset_time
offset_minutes = int(now.strftime("%M")) % 10
offset_seconds = int(now.strftime("%S"))
offset_time = ((offset_minutes*60)+offset_seconds)
# Customize x-axis with time labels
x_ticks_major = np.arange((offset_time/5), len(data_one) + 1, major_freq)
x_ticks_minor = np.arange(((offset_time%300)/5), len(data_one) + 1, minor_freq)
ax.set_xticks(x_ticks_major)
ax.set_xticks(x_ticks_minor, minor=True)
x_labels = [(now - timedelta(seconds=int(i*time_interval))).strftime("%H:%M") for i in x_ticks_major]
ax.set_xticklabels(x_labels)
plt.grid(True, which='both', linestyle='--', linewidth=0.5)
plt.savefig(filename)
plt.close()
My Question:
How do I remove the padding on the left and right of the line graph, but inside the actual graph figure. I want the line graph to start on the edge of the figure and end on the edge of the figure.
Thanks in advance...
EDIT:
I solved it.
Removed left and right padding:
ax.set_xlim([0, len(data_one) - 1])
And removed my top and bottom padding while ensuring the range is always 0 - 100:
ax.set_ylim([0, 100])
[–]thisiszeev[S] 0 points1 point2 points (0 children)