change the width and heigh of iplot

108 Views Asked by At

my code is so simple

this is my code

import cufflinks as cf
import pandas as pd
cf.go_offline()
cf.set_config_file(offline=False, world_readable=True)

@st.cache
def get_data(url):
    df = pd.read_csv(url)
    df["date"] = pd.to_datetime(df.date).dt.date
    df['date'] = pd.DatetimeIndex(df.date)

    return df

url = "https://covid.ourworldindata.org/data/owid-covid-data.csv"
data = get_data(url)

daily_cases = data.groupby(pd.Grouper(key="date", freq="1D")).aggregate(new_cases=("new_cases", "sum")).reset_index()
fig = daily_cases.iplot(kind="line", asFigure=True,
                        x="date", y="new_cases")
st.plotly_chart(fig)

the result of my code is something like :

enter image description here

now i want to change the height and width of the plot and also change the background od it to white and i want change the color of line to two color half of it to blue and othe half to red

thanks in advance for your help and advaice

1

There are 1 best solutions below

0
On

One way to do this is to define the layout you want to have before plotting everything. So for example, if you want a 500 by 600 plot, with some left alignment of the the xaxis and yaxis and some title of you choice, do this:

title = 'Cases'
layout = dict(width=500,
        height=600, xaxis =dict(side='left'), yaxis=dict(side='left'), title=title)

daily_cases = data.groupby(pd.Grouper(key="date", freq="1D")).aggregate(new_cases=("new_cases", "sum")).reset_index()
fig = daily_cases.iplot(kind="line", asFigure=True, x="date", y="new_cases", layout=layout)

will give you

enter image description here

to change the background color:

layout = dict(plot_bgcolor='rgba(0,0,0,0)', width=500,
        height=600, xaxis =dict(side='left'), yaxis=dict(side='left'), title=title)

daily_cases = data.groupby(pd.Grouper(key="date", freq="1D")).aggregate(new_cases=("new_cases", "sum")).reset_index()
fig = daily_cases.iplot(kind="line", asFigure=True, x="date", y="new_cases", layout=layout)

which will give you a white background.