下面的示例将数据绘制为折线图,并使用颜色
SUB_TYPE
.
这个
articles_binned
列的格式为字符串。如果它不适用于您的数据,请考虑在问题中包含一些示例数据以帮助调试。
更新
:OP确认转换
文章_装箱
列到字符串解决了这个问题。
SUB_TYPE articles_binned Cancellation_Percentage
0 Type A (0, 5] 14
1 Type A (5, 15] 21
...
14 Type D (15, 25] 14
15 Type D (25, 50] 13
可复制示例
import pandas as pd
#
# Data for testing
#
data = {
'SUB_TYPE': [
'Type A', 'Type A', 'Type A', 'Type A',
'Type B', 'Type B', 'Type B', 'Type B',
'Type C', 'Type C', 'Type C', 'Type C',
'Type D', 'Type D', 'Type D', 'Type D'
],
'articles_binned': [
'(0, 5]', '(5, 15]', '(15, 25]', '(25, 50]',
'(0, 5]', '(5, 15]', '(15, 25]', '(25, 50]',
'(0, 5]', '(5, 15]', '(15, 25]', '(25, 50]',
'(0, 5]', '(5, 15]', '(15, 25]', '(25, 50]'
],
'Cancellation_Percentage': [
14, 21, 14, 13,
16, 25, 18, 17,
21, 21, 19, 12,
15, 16, 14, 13
]
}
df = pd.DataFrame(data)
display(df)
#
# Plot
#
import seaborn as sns
import matplotlib.pyplot as plt
#Bar plot
ax = sns.barplot(
df, x='articles_binned', y='Cancellation_Percentage',
hue='SUB_TYPE', legend=False
)
sns.despine(ax.figure)
ax.figure.set_size_inches(5, 2.5)
plt.show()
#Line plot
ax = sns.lineplot(
df, x='articles_binned', y='Cancellation_Percentage',
hue='SUB_TYPE',
marker='s', linewidth=1,
)
ax.figure.set_size_inches(5, 3)
sns.despine(ax.figure)
plt.show()