情节:如何绘制共享x轴的多条线?

时间:2020-08-05 12:55:22

标签: python plotly plotly-dash

我想在同一个画布上用相同的x轴绑定多条线,如figure中所示:

enter image description here

使用子图无法实现预期的愿望。

import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=2, shared_xaxes=True,vertical_spacing=0.1)
fig.add_scatter(y=[2, 1, 3], row=1, col=1)
fig.add_scatter(y=[1, 3, 2], row=2, col=1)
fig.show()

我可以知道如何做到这一点,如果有人可以指出好的阅读材料,请多谢

2 个答案:

答案 0 :(得分:4)

使用this这样的数据集,您可以选择任意数量的列,使用fig = make_subplots()shared_xaxes设置为True来设置图形,然后使用循环中使用fig.add_trace(go.Scatter(x=df[col].index, y=df[col].values), row=i, col=1)的共享x轴:

enter image description here

让我知道这是否是您可以使用的设置,但需要进行一些调整。

完整代码:

import plotly.graph_objects as go
import plotly.io as pio
from plotly.subplots import make_subplots
import pandas as pd

# data
pio.templates.default = "plotly_white"
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df = df.set_index('Date')
df.tail()
cols = df.columns[:-4]
ncols = len(cols)

# subplot setup
fig = make_subplots(rows=ncols, cols=1, shared_xaxes=True)

for i, col in enumerate(cols, start=1):
    fig.add_trace(go.Scatter(x=df[col].index, y=df[col].values), row=i, col=1)
    
fig.show()

答案 1 :(得分:2)

根据您绘制的数据,我认为您可以在https://plotly.com/python/subplots/上签出“带有共享X轴(低级API)的堆叠子图”

或者通过向上移动每个线条图来分离数据,如下所示:

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
    //custom log 1
    _logger.LogInformation($"Reached Into Register Method - {DateTime.UtcNow.ToString()}");

    returnUrl = returnUrl ?? Url.Content("~/");
    ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
    if (ModelState.IsValid)
    {
        var user = new IdentityUser { UserName = Input.Email, Email = Input.Email };
        var result = await _userManager.CreateAsync(user, Input.Password);
                

        if (result.Succeeded)
        {
            _logger.LogInformation("User created a new account with password.");

            //custom log 2
            _logger.LogInformation($"Created New User - {DateTime.UtcNow.ToString()}");

            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
            code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
            var callbackUrl = Url.Page(
                "/Account/ConfirmEmail",
                pageHandler: null,
                values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                protocol: Request.Scheme);

            //custom log 3
            _logger.LogInformation($"Generated Email Token - {DateTime.UtcNow.ToString()}");

            await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

            //custom log 4
            _logger.LogInformation($"Sent Email - {DateTime.UtcNow.ToString()}");

            if (_userManager.Options.SignIn.RequireConfirmedAccount)
            { 
               //...