如何在 Python 中使用 Plotly Express 在同一 y 轴上绘制多条线 [英] How to plot multiple lines on the same y-axis using Plotly Express in Python

查看:81
本文介绍了如何在 Python 中使用 Plotly Express 在同一 y 轴上绘制多条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了plotly express.我正在尝试做一些简单的事情 - 将我的数据框的每一列绘制在同一个 y 轴上,索引作为 x 轴.以下是问题/观察:

数据框是否有必要将索引作为列用作 x 轴?我可以不直接使用 x 轴的索引吗?如何为同一个 x 轴在 y 轴上添加多个轨迹?

请注意,我不是在尝试使用 plotly 添加跟踪,而是尝试使用 plotly-express.

另外,网上也有一些类似的帖子,最接近的是

解决方案

您的代码运行良好!但是如果你特别不想将(有点费力的)add_trace() 函数应用到每一行,你可以使用px.line().这曾经要求您将数据从

如果您想知道如何对长格式的数据做同样的事情,以下是使用 Pandas 和 plotly 的方法:

代码 2:

# 导入导入 plotly.express 作为 px将熊猫导入为 pd将 numpy 导入为 np# 数据df_wide = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')df_long=pd.melt(df_wide, id_vars=['Date'], value_vars=['AAPL.Open', 'AAPL.High', 'AAPL.Low', 'AAPL.Close', 'mavg'])# 情节fig = px.line(df_long, x='Date', y='value', color='variable')# 显示情节图.show()

I just installed plotly express. And I am trying to do something simple - plot each column of my data frame on the same y-axis with the index as x-axis. Here are questions/observations:

Is it necessary for the data frame to have index as a column to be used as x-axis ? Can I not directly use the index for x-axis? How can I add multiple traces as were called in plotly on y-axis for the same x-axis ?

Please note that, I am not trying to add traces using plotly, rather trying to use plotly-express.

Also, there a few similar posts online, the closest was this: https://community.plot.ly/t/multiple-traces-plotly-express/23360 However, this post shows how you can add a scatter, not a line. I want to plot a line and there is no add_line similar to add_scatter shown in the example here.

Appreciate any help in advance

Sample code:

import plotly.express as px 
import pandas as pd 
import numpy as np 

# Get some data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

# Plot 
fig = px.line(df, x='Date', y='AAPL.High')

# Only thing I figured is - I could do this 
fig.add_scatter(x=df['Date'], y=df['AAPL.Low']) # Not what is desired - need a line 

# Show plot 
fig.show()

PLot:

解决方案

Your code works fine! But if you specifically do not want to apply the (somewhat laborious) add_trace() function to each line, you can use px.line(). This used to require you to transform your data from a wide to long format. But not anymore, so just define an index and name the columns you'd like to plot. Or reference all or a subset of your dataframe columns through, for ecxample, y=df.columns[1:-6]

Code 1:

# imports
import plotly.express as px 
import pandas as pd 
import numpy as np 

# data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = px.line(df, x='Date', y=df.columns[1:-6])

# Show plot 
fig.show()

Plot:

If you'd like to know how to do the same thing with data of a long format, here's how you do that too using pandas and plotly:

Code 2:

# imports
import plotly.express as px 
import pandas as pd 
import numpy as np 

# data
df_wide = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df_long=pd.melt(df_wide, id_vars=['Date'], value_vars=['AAPL.Open', 'AAPL.High', 'AAPL.Low', 'AAPL.Close', 'mavg'])

# plotly 
fig = px.line(df_long, x='Date', y='value', color='variable')

# Show plot 
fig.show()

这篇关于如何在 Python 中使用 Plotly Express 在同一 y 轴上绘制多条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆