0

I have a plot in Matplotlib and I would like to increase the margins between the ticks on the x-axis by increasing the widht of the diagram. I tried to use the code mentioned here

plt.figure(figsize=(20,10)) --> did not seem to have any effect

fig, ax = plt.subplots(figsize=(20, 10)) --> created a diagram with an empty plot

Here is my code :

from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
import numpy as np
load = [0.0, 0.1, 0.5, 0.7, 0.4, 0.5, 0.4, 0.3, 0.4, 0.5, 0.65, 0.75, 0.8, 0.7, 0.25, 0.1, 0.1, 0.75, 0.2, 0.0, 0.0, 0.0, 0.5, 0.65, 0.5]
hours = list(range(25)) # [0, 1, 2, ... 22, 23, 24]
labels = [f'{h:02d}:00' for h in hours] # ["00:00", "01:00", ... "23:00", "24:00"]


f = interp1d(hours, load)
f2 = interp1d(hours, load, kind='cubic')

xnew = np.linspace(0, 24, num=500, endpoint=True)




plt.xticks(np.arange(0, 25, step=1))  # Set label locations.
plt.xticks(np.arange(25), labels)  # Set text labels.
plt.xticks(np.arange(25), labels, rotation=90, fontsize=13)
plt.xlim(xmin=0.0)

plt.plot(xnew, f2(xnew), color="gold", linewidth=3)
plt.legend(['Generation'], loc='best')

plt.legend( ['Electricity generation'], bbox_to_anchor=(0.5, 1.2), loc='upper center' , fontsize=16)
plt.ylabel("Electrical power", fontsize=18, labelpad=8)
plt.xlabel("Time of day", fontsize=18, labelpad=8)
plt.xlim(xmin=0.0)
plt.tick_params(labelleft=False)

plt.savefig('Generation_2.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()

Can you tell me how I can do this? I'd appreciate every comment and will be thankful for your help.

Anurag Reddy
  • 1,159
  • 11
  • 19
VanessaF
  • 515
  • 11
  • 36
  • Note that both `plt.figure()` and `plt.subplots()` create a **new** plot figure with the given size (or a default size). So, they must be the first plot-related functions to be called. – JohanC Aug 31 '20 at 20:59

1 Answers1

2

For me, putting plt.figsize() before any plot command works:

plt.figure(figsize=(20,10))

# all other plot command    
plt.xticks(np.arange(0, 25, step=1))  # Set label locations.
plt.xticks(np.arange(25), labels)  # Set text labels.
plt.xticks(np.arange(25), labels, rotation=90, fontsize=13)
plt.xlim(xmin=0.0)

plt.plot(xnew, f2(xnew), color="gold", linewidth=3)
plt.legend(['Generation'], loc='best')

plt.legend( ['Electricity generation'], bbox_to_anchor=(0.5, 1.2), loc='upper center' , fontsize=16)
plt.ylabel("Electrical power", fontsize=18, labelpad=8)
plt.xlabel("Time of day", fontsize=18, labelpad=8)
plt.xlim(xmin=0.0)
plt.tick_params(labelleft=False)

plt.savefig('Generation_2.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()

Output:

enter image description here

Versus without:

enter image description here

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • Thanks for the answer Quang. It also works for me. Maybe a follow up question. As I do bot want to Display the y-Ticks is there a way how I van remove the small horizontal lines on the y-axis? – VanessaF Sep 01 '20 at 08:08
  • Is it possible to remove the y-axis-ticks (but not the name)? – VanessaF Sep 01 '20 at 19:31