简体   繁体   中英

change the width and heigh of iplot

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:

在此处输入图像描述

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

One way to do this is to define the layout you want to have before plotting evrything. So for example, if you want a 1000 by 1000 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

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM