My Line graph dosen't stretch to the end of the border but instead starts delayed and ends before the terminating grid line. I have researched this problem but haven't gotten an answer. The following is the code for my chart:
plt.style.use("custom_style.mplstyle")
fig = plt.figure(figsize=(16, 9), dpi=240)
ax = fig.add_subplot(1, 7, (1, 6))
table = fig.add_subplot(1, 7, 7)
# Delete the grid lines from the table column
table = plt.gca()
table.get_xaxis().set_visible(False)
table.get_yaxis().set_visible(False)
plt.box(on=None)
# Create and scale table
CellColor = []
[CellColor.append(["#FFFFFF07"]) for row in range(stats.shape[0])]
theTable = table.table(cellText=stats.values, cellColours=CellColor, loc="center", cellLoc="left")
theTable.scale(1.4, 6)
theTable.set_fontsize(20)
color = "#52cc00" if float(dif) >= 0 else "#cc5240"
ax.plot_date(df["time"], df["close"], fmt=color, xdate=False, marker=None, linewidth=3, linestyle="-")
dateFMT = mdates.DateFormatter(r'%I%p')
ax.xaxis.set_major_formatter(dateFMT)
chart_name = name if isinstance(chart_name, pd.core.series.Series) else chart_name
ax.set_title(f"{chart_name}: {close} [{dif}%]", loc="left")
plt.savefig(f"charts/{name}.png")
where the contents of custom_style.mplstyle are
### FONT
text.color : white
font.family : Tahoma
### AXES
axes.facecolor : FFFFFF07 # axes background color
axes.edgecolor : FFFFFF3D # axes edge color
axes.labelcolor : FFFFFFD9
### TICKS
xtick.color : FFFFFFD9 # color of the tick labels
ytick.color : FFFFFFD9 # color of the tick labels
### GRIDS
grid.color : E6E6FA # grid color
axes.grid : True
grid.linewidth : 0.4
### Legend
legend.facecolor : inherit # legend background color (when 'inherit' uses axes.facecolor)
legend.edgecolor : FFFFFFD9 # legend edge color (when 'inherit' uses axes.edgecolor)
### FIGURE
#TODO: notebook ignores this
figure.facecolor : 0C1C23 # figure facecolor
savefig.facecolor : 0C1C23 # figure facecolor
axes.titlepad : 12
### Title
axes.titlesize : 25
axes.titlelocation : left
CodePudding user response:
All you need to do is set the x
margins to 0
with plt.margins(x=0)
:
L1 = [1,2,3,4,5,6]
plt.plot(L1) # Default margins
plt.show()
plt.plot(L1)
plt.margins(x=0) # 0 Margins
plt.show()
CodePudding user response:
This delay is caused by the x
and y
margins that are non zero by default. Adding up on other answers, you can set this up directly in the custom_style.mplstyle
file with
axes.xmargin: 0
axes.ymargin: 0
I here reproduced your case with taking fake data on approximately the same time span and leading to the above image.
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
plt.style.use("custom_style.mplstyle")
# Figure and axes instance
fig = plt.figure(figsize=(16, 9), dpi=240)
ax = fig.add_subplot(1, 7, (1, 6))
table = fig.add_subplot(1, 7, 7)
# Delete the grid lines from the table column
table = plt.gca()
table.get_xaxis().set_visible(False)
table.get_yaxis().set_visible(False)
plt.box(on=None)
# Plot
color = "#52cc00"
dates = np.arange(
np.datetime64("2022-01-01 09:00"), np.datetime64("2022-01-01 11:00")
)
data = np.random.rand(len(dates))
ax.plot_date(
dates, data, fmt=color, xdate=False, marker=None, linewidth=3, linestyle="-"
)
# Format dates
dateFMT = mdates.DateFormatter(r"%I%p")
ax.xaxis.set_major_formatter(dateFMT)