python - 在Python中,绘制具有相同x轴和y轴的不同类别数据的方法是什么?

标签 python plotly data-visualization seaborn data-science

背景信息:我正在使用消费者金融保护局的消费者投诉数据集。我想通过绘制 y 上的投诉计数和 x 上的时间来绘制投诉时间序列图,为每种类型的投诉绘制一条线。我想看看这些值(value)观如何随着时间的推移而变化。到目前为止,我已经尝试过 Seaborn 和 Plotly。以下是 Plotly 中的内容。

trace1 = go.Scatter(x=df.DateReceived,
                    y=df.Sum,
                    name = "Time Series of Types of Complaints",
                    line = dict(color = 'blue'),
                    opacity = 0.4)

layout = dict(title='Time Series of Types of Complaints',)

fig = dict(data=[trace1], layout=layout)
iplot(fig)

Attempted Plot

数据框如下所示:

data = {'Date': ['2011-12-01', '2011-12-06', '2011-12-06', '2011-12-07', '2011-12-07'], 'Issue':  ['Loan Modification', 'Loan Servicing', 'Loan Servicing', 'Loan Modification', 'Loan Servicing'], 'Sum': [1, 1, 2, 2, 3]}

df = pd.DataFrame(data)

我知道我的 plotly 中的问题在于它将所有不同的总和连接在一起而不是分开它们。

我知道我可以针对每种不同类型的投诉将每个总和分成不同的列。然后手动添加每个跟踪,执行如下操作(取自 Plotly 网站):

fig.add_trace(go.Scatter(
                x=df.Date,
                y=df['AAPL.Low'],
                name="AAPL Low",
                line_color='dimgray',
                opacity=0.8))

但是必须有一种更简单/更少暴力的方法,我可以将所有总和保留在一列中,并按问题类型进行描述。

最佳答案

为了说明像您这样的数据集随着时间的推移的发展,实际上没有必要引入一个类别作为颜色变化,如 sns.lmplot(data=movies, x='CriticRating',y ='AudienceRating', fit_reg=False, Hue='Genre') 就可以了。由于这不是真正的问题,只是在评论中简要提到,所以我会坚持使用分组(或堆叠)条形图或折线图。以下是如何使用plotly 来完成此操作。

堆积柱形图:

分组柱形图的绘图:

enter image description here

分组柱形图代码:

# imports
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
data = {'Date': ['2011-12-01', '2011-12-06', '2011-12-06', '2011-12-07', '2011-12-07'], 'Issue':  ['Loan Modification', 'Loan Servicing', 'Loan Servicing', 'Loan Modification', 'Loan Servicing'], 'Sum': [1, 1, 2, 2, 3]}
df = pd.DataFrame(data)

# build figure
fig=go.Figure(data=[go.Bar(name='Modification',
                          x=df[df['Issue']=='Loan Modification']['Date'],
                          y=df[df['Issue']=='Loan Modification']['Sum']),
                   
                    go.Bar(name='Servicing',
                          x=df[df['Issue']=='Loan Servicing']['Date'],
                          y=df[df['Issue']=='Loan Servicing']['Sum'])])


# Change the bar mode
fig.update_layout(barmode='group')
#fig.update_layout(barmode='stack')

# show figure
fig.show()

堆积柱形图的绘图:

enter image description here

堆积柱形图代码:

只需使用与上面相同的代码片段,但包含该行

fig.update_layout(barmode='stack') 就在 fig.show()

之前

折线图:

只需将 go.Bar() 替换为 go.Scatter() 即可得到:

折线图绘制:

enter image description here

折线图代码:

# imports
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
data = {'Date': ['2011-12-01', '2011-12-06', '2011-12-06', '2011-12-07', '2011-12-07'], 'Issue':  ['Loan Modification', 'Loan Servicing', 'Loan Servicing', 'Loan Modification', 'Loan Servicing'], 'Sum': [1, 1, 2, 2, 3]}
df = pd.DataFrame(data)

# build figure
fig=go.Figure(data=[go.Scatter(name='Modification',
                          x=df[df['Issue']=='Loan Modification']['Date'],
                          y=df[df['Issue']=='Loan Modification']['Sum']),
                   
                    go.Scatter(name='Servicing',
                          x=df[df['Issue']=='Loan Servicing']['Date'],
                          y=df[df['Issue']=='Loan Servicing']['Sum'])])

# show figure
fig.show()

如果你觉得这个 plotly 有点奇怪,我完全同意。但这是因为您在数据样本中的 2012-12-16 上获得了两个关于 Loan Servicing 的观察结果。您可以通过在绘图之前正确分组数据框来解决这个问题。

希望这有帮助!如果这不适合您,请随时告诉我!

关于python - 在Python中,绘制具有相同x轴和y轴的不同类别数据的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59021609/

相关文章:

python - 如何使用 python 将参数传递给 bit.ly api?

r - 如何从 R 绘图图中获取坐标

r - 向图节点添加标签

Python Pandas Timeseries 如何找到值高于特定值的最大序列

python - 'ToPILImage' 对象没有属性 'show'

python - 在 Python 中反转堆栈

Plotly box p 值显着注释

Python Plotly - 向箱线图添加水平线

r - 由线条连接的条形图/如何连接在 R/ggplot2 中使用 grid.arrange 排列的两个图形

c# - Linqpad 图表。 Column 和 StackedColumn 的组合