Reputation: 1131
the following code is an example, because i am not able to reproduce the problem. the image 2 shows my problem, that the lines start not on the left (on yaxis). the input data seem to be the same, the code to plot the chart is exactly the same.
import pandas as pd
from pandas.tseries.offsets import DateOffset
sdate = date(2021,1,31)
edate = date(2021,8,30)
date_range = pd.date_range(sdate,edate-timedelta(days=1),freq='m')
df_test = pd.DataFrame({ 'Datum': date_range})
df_test.loc[len(df_test)+1,'Datum']=(df_test['Datum'].min()-DateOffset(months=1))
df_test=df_test.sort_values(by='Datum',ignore_index=True)
df_test['indexed'] = [1,2,3,4,5,6,7,7]
df_test['index'] = [5,5,3,4,5,6,7,7]
data = df_test
sdate=data['Datum'].min()
title = 'Gesamtperformance'
font ='Helvetica'
fig = go.Figure()
fig.add_trace(go.Scatter(x=data['Datum'], y=data['indexed'],
line = dict(color='blue', width=4),
mode='lines',
name=title))
fig.add_trace(go.Scatter(x=data['Datum'], y=data['index'],
line=dict(color='black', width=4, dash='dot'),
mode='lines',
name=title))
fig.layout = go.Layout(yaxis=dict(tickformat=".0%"))
fig.update_layout(font_family=font,
font=dict(
family=font,
size=30),
legend=dict(font=dict(family=font, size=30, color="black")))
fig.update_layout(yaxis_title="Performance",
margin=dict(l=100, r=0, t=0, b=120),
plot_bgcolor='grey',
legend=dict(
orientation="h",
yanchor="top",
y=-0.065, # distance legend to chart
xanchor="auto",
x=0.5)
)
fig.update_xaxes(dtick="M6", tickformat="%b %Y")
fig.update_layout(xaxis=dict(tick0=sdate)) # sets start date
fig.update_layout(width=1485, height=1100)
plotly.io.write_image(fig, file='test_line.png', format='png')
output example code, where the lines begin on yaxis:
output, where lines do not start in yaxis (left) and I do not why:
may somebody know how to start the lines on the left. thanks
Upvotes: 0
Views: 79
Reputation: 1313
Consider using x_axis_range
in the update_layout
. The code is as follows:
fig.update_layout(xaxis_range=[sdate, edate])
Upvotes: 1