gpt4 book ai didi

python - Plotly:如何在指定阈值的折线图上显示不同的颜色段?

转载 作者:行者123 更新时间:2023-12-04 12:57:35 27 4
gpt4 key购买 nike

我有一个多线图,显示随时间增加的百分比。我想在我的代码中设置一个阈值来设置上限和下限。如果线条超出这些范围,我希望折线图的特定部分与其父项的颜色不同。
这就是我正在做的:

import plotly.express as px
import plotly.graph_objects as go



fig = px.line(df14, x = "Date", y = "Percent", color = "POD",
title = "POD Growth in Percentage (US)",
labels = {"Percent": "Percent Growth"})

fig.update_layout(
font_family="Arial",
font_color="black",
title_font_family="Arial",
title_font_color="black",
legend_title_font_color="black" #style the text (legend, title etc)
)


fig.update_xaxes(title_font_family="Arial") #style ance center title
fig.update_layout(
title={
'text': "POD Growth Percentage in US (Line Graph)",
'y':0.9,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'})


fig.update_traces(mode='markers+lines') #add dots to line

fig.show()
这是视觉结果:
![image|690x328](upload://hujUQbdPtbCAiXQoTTwLmzAAi02.png)
让我放大一行以更好地解释:
我想要为每个 POD 设置一个 阈值,并且 如果该线高于或低于此阈值,则 图表部分的颜色将不同。例如:
POD IAD的上限可以是5,IAD的下限可以是0。
任何大于 5 或​​小于 0 的值都会以特定颜色突出显示。
POD SJC 的上限可能是 10,SJC 的下限可能是 3
任何大于 10 或小于 3 的值都应以特定颜色突出显示。
我想要每个 POD 的阈值
请参阅以下内容:
enter image description here
线条中突出显示的黄色部分反射(reflect)了折线图超出或降低了设定阈值的位置。使用 Plotly 可以做到这一点吗?
这是原始数据示例: 更新:
    POD      Start      End         Diff        Percent     Date        
IAD 4/1/2019 5/1/2019 160.4279 11.10809 04-01-2019 to 05-01-2019
IAD 5/1/2019 6/1/2019 136.0248 8.476798 05-01-2019 to 06-01-2019
IAD 6/1/2019 7/1/2019 174.0513 9.998946 06-01-2019 to 07-01-2019
IAD 7/1/2019 8/1/2019 112.0424 5.851551 07-01-2019 to 08-01-2019
IAD 8/1/2019 9/1/2019 141.8488 6.998691 08-01-2019 to 09-01-2019
IAD 9/1/2019 10/1/2019 103.5522 4.774984 09-01-2019 to 10-01-2019
IAD 10/1/2019 11/1/2019 125.6087 5.528085 10-01-2019 to 11-01-2019
IAD 11/1/2019 12/1/2019 145.2591 6.058016 11-01-2019 to 12-01-2019
IAD 12/1/2019 1/1/2020 115.5121 4.542251 12-01-2019 to 01-01-2020
IAD 1/1/2020 2/1/2020 185.7191 6.985673 01-01-2020 to 02-01-2020
IAD 2/1/2020 3/1/2020 126.7386 4.455896 02-01-2020 to 03-01-2020
IAD 3/1/2020 4/1/2020 231.3461 7.786734 03-01-2020 to 04-01-2020
IAD 4/1/2020 5/1/2020 97.02587 3.02981 04-01-2020 to 05-01-2020
IAD 5/1/2020 6/1/2020 42.85235 1.298792 05-01-2020 to 06-01-2020
IAD 6/1/2020 7/1/2020 124.666 3.729997 06-01-2020 to 07-01-2020
IAD 7/1/2020 8/1/2020 357.9974 10.32609 07-01-2020 to 08-01-2020
IAD 8/1/2020 9/1/2020 490.9587 12.8358 08-01-2020 to 09-01-2020
IAD 9/1/2020 10/1/2020 204.5478 4.739428 09-01-2020 to 10-01-2020
IAD 10/1/2020 11/1/2020 287.6025 6.362292 10-01-2020 to 11-01-2020
SJCtest 4/1/2019 5/1/2019 12.38486 5.780551 04-01-2019 to 05-01-2019
SJCtest 5/1/2019 6/1/2019 -2.61735 -1.15487 05-01-2019 to 06-01-2019
SJCtest 6/1/2019 7/1/2019 -5.6187 -2.50814 06-01-2019 to 07-01-2019
SJCtest 7/1/2019 8/1/2019 3.204252 1.467153 07-01-2019 to 08-01-2019
SJCtest 8/1/2019 9/1/2019 -25.3782 -11.4521 08-01-2019 to 09-01-2019
SJCtest 9/1/2019 10/1/2019 -10.9717 -5.59137 09-01-2019 to 10-01-2019
更新的原始数据
> {'Unnamed: 0': {0: 1,   1: 2,   2: 3,   3: 4,   4: 5,   5: 6,   6: 7, 
> 7: 8, 8: 9, 9: 10, 10: 11, 11: 12, 12: 13, 13: 14, 14:
> 15, 15: 16, 16: 17, 17: 18, 18: 19, 19: 21, 20: 22, 21:
> 23, 22: 24, 23: 25, 24: 26, 25: 27, 26: 28, 27: 29, 28:
> 30, 29: 31, 30: 32, 31: 33, 32: 34, 33: 35, 34: 36, 35:
> 37, 36: 38, 37: 40, 38: 41, 39: 42, 40: 43, 41: 44, 42:
> 45, 43: 46, 44: 47, 45: 48, 46: 49, 47: 50, 48: 51, 49:
> 52, 50: 53, 51: 54, 52: 55, 53: 56, 54: 57, 55: 58, 56:
> 60, 57: 61, 58: 62, 59: 63, 60: 64, 61: 65, 62: 66, 63:
> 67, 64: 68, 65: 69, 66: 70, 67: 71, 68: 72, 69: 73, 70:
> 74, 71: 75, 72: 76, 73: 77, 74: 78, 75: 80, 76: 81, 77:
> 82, 78: 83, 79: 84, 80: 85, 81: 86, 82: 87, 83: 88, 84:
> 89, 85: 90, 86: 91, 87: 92, 88: 93, 89: 94, 90: 95, 91:
> 96, 92: 97, 93: 98, 94: 100, 95: 101, 96: 102, 97: 103,
> 98: 104, 99: 105, 100: 106, 101: 107, 102: 108, 103: 109,
> 104: 110, 105: 111, 106: 112, 107: 113, 108: 114, 109: 115,
> 110: 116, 111: 117, 112: 118, 113: 120, 114: 121, 115: 122,
> 116: 123, 117: 124, 118: 125, 119: 126, 120: 127, 121: 128,
> 122: 129, 123: 130, 124: 131, 125: 132, 126: 133, 127: 134,
> 128: 135, 129: 136, 130: 137, 131: 138, 132: 140, 133: 141,
> 134: 142, 135: 143, 136: 144, 137: 145, 138: 146, 139: 147,
> 140: 148, 141: 149, 142: 150, 143: 151, 144: 152, 145: 153,
> 146: 155, 147: 156, 148: 157, 149: 158, 150: 159, 151: 160,
> 152: 161, 153: 162, 154: 163, 155: 164, 156: 165, 157: 166,
> 158: 167, 159: 168, 160: 169, 161: 170, 162: 171, 163: 172,
> 164: 173}, 'POD': {0: 'IAD ', 1: 'IAD ', 2: 'IAD ', 3: 'IAD ',
> 4: 'IAD ', 5: 'IAD ', 6: 'IAD ', 7: 'IAD ', 8: 'IAD ', 9:
> 'IAD ', 10: 'IAD ', 11: 'IAD ', 12: 'IAD ', 13: 'IAD ', 14:
> 'IAD ', 15: 'IAD ', 16: 'IAD ', 17: 'IAD ', 18: 'IAD ', 19:
> 'SJCtest ', 20: 'SJCtest ', 21: 'SJCtest ', 22: 'SJCtest ',
> 23: 'SJCtest ', 24: 'SJCtest ', 25: 'SJCtest ', 26: 'SJCtest ',
> 27: 'SJCtest ', 28: 'SJCtest ', 29: 'SJCtest ', 30: 'SJCtest ',
> 31: 'SJCtest ', 32: 'SJCtest ', 33: 'SJCtest ', 34: 'SJCtest ',
> 35: 'SJCtest ', 36: 'SJCtest ', 37: 'SJCtest2 ', 38: 'SJCtest2
> ', 39: 'SJCtest2 ', 40: 'SJCtest2 ', 41: 'SJCtest2 ', 42:
> 'SJCtest2 ', 43: 'SJCtest2 ', 44: 'SJCtest2 ', 45: 'SJCtest2 ',
> 46: 'SJCtest2 ', 47: 'SJCtest2 ', 48: 'SJCtest2 ', 49: 'SJCtest2
> ', 50: 'SJCtest2 ', 51: 'SJCtest2 ', 52: 'SJCtest2 ', 53:
> 'SJCtest2 ', 54: 'SJCtest2 ', 55: 'SJCtest2 ', 56: 'SJCtest3 ',
> 57: 'SJCtest3 ', 58: 'SJCtest3 ', 59: 'SJCtest3 ', 60: 'SJCtest3
> ', 61: 'SJCtest3 ', 62: 'SJCtest3 ', 63: 'SJCtest3 ', 64:
> 'SJCtest3 ', 65: 'SJCtest3 ', 66: 'SJCtest3 ', 67: 'SJCtest3 ',
> 68: 'SJCtest3 ', 69: 'SJCtest3 ', 70: 'SJCtest3 ', 71: 'SJCtest3
> ', 72: 'SJCtest3 ', 73: 'SJCtest3 ', 74: 'SJCtest3 ', 75:
> 'SJCtest4 ', 76: 'SJCtest4 ', 77: 'SJCtest4 ', 78: 'SJCtest4 ',
> 79: 'SJCtest4 ', 80: 'SJCtest4 ', 81: 'SJCtest4 ', 82: 'SJCtest4
> ', 83: 'SJCtest4 ', 84: 'SJCtest4 ', 85: 'SJCtest4 ', 86:
> 'SJCtest4 ', 87: 'SJCtest4 ', 88: 'SJCtest4 ', 89: 'SJCtest4 ',
> 90: 'SJCtest4 ', 91: 'SJCtest4 ', 92: 'SJCtest4 ', 93: 'SJCtest4
> ', 94: 'SJCtest5 ', 95: 'SJCtest5 ', 96: 'SJCtest5 ', 97:
> 'SJCtest5 ', 98: 'SJCtest5 ', 99: 'SJCtest5 ', 100: 'SJCtest5 ',
> 101: 'SJCtest5 ', 102: 'SJCtest5 ', 103: 'SJCtest5 ', 104:
> 'SJCtest5 ', 105: 'SJCtest5 ', 106: 'SJCtest5 ', 107: 'SJCtest5
> ', 108: 'SJCtest5 ', 109: 'SJCtest5 ', 110: 'SJCtest5 ', 111:
> 'SJCtest5 ', 112: 'SJCtest5 ', 113: 'SJCtest6 ', 114: 'SJCtest6
> ', 115: 'SJCtest6 ', 116: 'SJCtest6 ', 117: 'SJCtest6 ', 118:
> 'SJCtest6 ', 119: 'SJCtest6 ', 120: 'SJCtest6 ', 121: 'SJCtest6
> ', 122: 'SJCtest6 ', 123: 'SJCtest6 ', 124: 'SJCtest6 ', 125:
> 'SJCtest6 ', 126: 'SJCtest6 ', 127: 'SJCtest6 ', 128: 'SJCtest6
> ', 129: 'SJCtest6 ', 130: 'SJCtest6 ', 131: 'SJCtest6 ', 132:
> 'SJCa ', 133: 'SJCa ', 134: 'SJCa ', 135: 'SJCa ', 136: 'SJCa
> ', 137: 'SJCa ', 138: 'SJCa ', 139: 'SJCa ', 140: 'SJCa ',
> 141: 'SJCa ', 142: 'SJCa ', 143: 'SJCa ', 144: 'SJCa ', 145:
> 'SJCa ', 146: 'SJCb ', 147: 'SJCb ', 148: 'SJCb ', 149: 'SJCb
> ', 150: 'SJCb ', 151: 'SJCb ', 152: 'SJCb ', 153: 'SJCb ',
> 154: 'SJCb ', 155: 'SJCb ', 156: 'SJCb ', 157: 'SJCb ', 158:
> 'SJCb ', 159: 'SJCb ', 160: 'SJCb ', 161: 'SJCb ', 162: 'SJCb
> ', 163: 'SJCb ', 164: 'SJCb '}, 'Start_Date': {0: '4/1/2019',
> 1: '5/1/2019', 2: '6/1/2019', 3: '7/1/2019', 4: '8/1/2019', 5:
> '9/1/2019', 6: '10/1/2019', 7: '11/1/2019', 8: '12/1/2019', 9:
> '1/1/2020', 10: '2/1/2020', 11: '3/1/2020', 12: '4/1/2020',
> 13: '5/1/2020', 14: '6/1/2020', 15: '7/1/2020', 16: '8/1/2020',
> 17: '9/1/2020', 18: '10/1/2020', 19: '4/1/2019', 20: '5/1/2019',
> 21: '6/1/2019', 22: '7/1/2019', 23: '8/1/2019', 24: '9/1/2019',
> 25: '10/1/2019', 26: '11/1/2019', 27: '12/1/2019', 28:
> '1/1/2020', 29: '2/1/2020', 30: '3/1/2020', 31: '4/1/2020',
> 32: '5/1/2020', 33: '6/1/2020', 34: '7/1/2020', 35: '8/1/2020',
> 36: '9/1/2020', 37: '4/1/2019', 38: '5/1/2019', 39: '6/1/2019',
> 40: '7/1/2019', 41: '8/1/2019', 42: '9/1/2019', 43: '10/1/2019',
> 44: '11/1/2019', 45: '12/1/2019', 46: '1/1/2020', 47:
> '2/1/2020', 48: '3/1/2020', 49: '4/1/2020', 50: '5/1/2020',
> 51: '6/1/2020', 52: '7/1/2020', 53: '8/1/2020', 54: '9/1/2020',
> 55: '10/1/2020', 56: '4/1/2019', 57: '5/1/2019', 58: '6/1/2019',
> 59: '7/1/2019', 60: '8/1/2019', 61: '9/1/2019', 62: '10/1/2019',
> 63: '11/1/2019', 64: '12/1/2019', 65: '1/1/2020', 66:
> '2/1/2020', 67: '3/1/2020', 68: '4/1/2020', 69: '5/1/2020',
> 70: '6/1/2020', 71: '7/1/2020', 72: '8/1/2020', 73: '9/1/2020',
> 74: '10/1/2020', 75: '4/1/2019', 76: '5/1/2019', 77: '6/1/2019',
> 78: '7/1/2019', 79: '8/1/2019', 80: '9/1/2019', 81: '10/1/2019',
> 82: '11/1/2019', 83: '12/1/2019', 84: '1/1/2020', 85:
> '2/1/2020', 86: '3/1/2020', 87: '4/1/2020', 88: '5/1/2020',
> 89: '6/1/2020', 90: '7/1/2020', 91: '8/1/2020', 92: '9/1/2020',
> 93: '10/1/2020', 94: '4/1/2019', 95: '5/1/2019', 96: '6/1/2019',
> 97: '7/1/2019', 98: '8/1/2019', 99: '9/1/2019', 100:
> '10/1/2019', 101: '11/1/2019', 102: '12/1/2019', 103:
> '1/1/2020', 104: '2/1/2020', 105: '3/1/2020', 106: '4/1/2020',
> 107: '5/1/2020', 108: '6/1/2020', 109: '7/1/2020', 110:
> '8/1/2020', 111: '9/1/2020', 112: '10/1/2020', 113: '4/1/2019',
> 114: '5/1/2019', 115: '6/1/2019', 116: '7/1/2019', 117:
> '8/1/2019', 118: '9/1/2019', 119: '10/1/2019', 120: '11/1/2019',
> 121: '12/1/2019', 122: '1/1/2020', 123: '2/1/2020', 124:
> '3/1/2020', 125: '4/1/2020', 126: '5/1/2020', 127: '6/1/2020',
> 128: '7/1/2020', 129: '8/1/2020', 130: '9/1/2020', 131:
> '10/1/2020', 132: '4/1/2019', 133: '5/1/2019', 134: '6/1/2019',
> 135: '7/1/2019', 136: '8/1/2019', 137: '9/1/2019', 138:
> '10/1/2019', 139: '11/1/2019', 140: '12/1/2019', 141:
> '1/1/2020', 142: '2/1/2020', 143: '3/1/2020', 144: '4/1/2020',
> 145: '5/1/2020', 146: '4/1/2019', 147: '5/1/2019', 148:
> '6/1/2019', 149: '7/1/2019', 150: '8/1/2019', 151: '9/1/2019',
> 152: '10/1/2019', 153: '11/1/2019', 154: '12/1/2019', 155:
> '1/1/2020', 156: '2/1/2020', 157: '3/1/2020', 158: '4/1/2020',
> 159: '5/1/2020', 160: '6/1/2020', 161: '7/1/2020', 162:
> '8/1/2020', 163: '9/1/2020', 164: '10/1/2020'}, 'End_Date': {0:
> '5/1/2019', 1: '6/1/2019', 2: '7/1/2019', 3: '8/1/2019', 4:
> '9/1/2019', 5: '10/1/2019', 6: '11/1/2019', 7: '12/1/2019', 8:
> '1/1/2020', 9: '2/1/2020', 10: '3/1/2020', 11: '4/1/2020', 12:
> '5/1/2020', 13: '6/1/2020', 14: '7/1/2020', 15: '8/1/2020',
> 16: '9/1/2020', 17: '10/1/2020', 18: '11/1/2020', 19:
> '5/1/2019', 20: '6/1/2019', 21: '7/1/2019', 22: '8/1/2019',
> 23: '9/1/2019', 24: '10/1/2019', 25: '11/1/2019', 26:
> '12/1/2019', 27: '1/1/2020', 28: '2/1/2020', 29: '3/1/2020',
> 30: '4/1/2020', 31: '5/1/2020', 32: '6/1/2020', 33: '7/1/2020',
> 34: '8/1/2020', 35: '9/1/2020', 36: '10/1/2020', 37: '5/1/2019',
> 38: '6/1/2019', 39: '7/1/2019', 40: '8/1/2019', 41: '9/1/2019',
> 42: '10/1/2019', 43: '11/1/2019', 44: '12/1/2019', 45:
> '1/1/2020', 46: '2/1/2020', 47: '3/1/2020', 48: '4/1/2020',
> 49: '5/1/2020', 50: '6/1/2020', 51: '7/1/2020', 52: '8/1/2020',
> 53: '9/1/2020', 54: '10/1/2020', 55: '11/1/2020', 56:
> '5/1/2019', 57: '6/1/2019', 58: '7/1/2019', 59: '8/1/2019',
> 60: '9/1/2019', 61: '10/1/2019', 62: '11/1/2019', 63:
> '12/1/2019', 64: '1/1/2020', 65: '2/1/2020', 66: '3/1/2020',
> 67: '4/1/2020', 68: '5/1/2020', 69: '6/1/2020', 70: '7/1/2020',
> 71: '8/1/2020', 72: '9/1/2020', 73: '10/1/2020', 74:
> '11/1/2020', 75: '5/1/2019', 76: '6/1/2019', 77: '7/1/2019',
> 78: '8/1/2019', 79: '9/1/2019', 80: '10/1/2019', 81:
> '11/1/2019', 82: '12/1/2019', 83: '1/1/2020', 84: '2/1/2020',
> 85: '3/1/2020', 86: '4/1/2020', 87: '5/1/2020', 88: '6/1/2020',
> 89: '7/1/2020', 90: '8/1/2020', 91: '9/1/2020', 92: '10/1/2020',
> 93: '11/1/2020', 94: '5/1/2019', 95: '6/1/2019', 96: '7/1/2019',
> 97: '8/1/2019', 98: '9/1/2019', 99: '10/1/2019', 100:
> '11/1/2019', 101: '12/1/2019', 102: '1/1/2020', 103: '2/1/2020',
> 104: '3/1/2020', 105: '4/1/2020', 106: '5/1/2020', 107:
> '6/1/2020', 108: '7/1/2020', 109: '8/1/2020', 110: '9/1/2020',
> 111: '10/1/2020', 112: '11/1/2020', 113: '5/1/2019', 114:
> '6/1/2019', 115: '7/1/2019', 116: '8/1/2019', 117: '9/1/2019',
> 118: '10/1/2019', 119: '11/1/2019', 120: '12/1/2019', 121:
> '1/1/2020', 122: '2/1/2020', 123: '3/1/2020', 124: '4/1/2020',
> 125: '5/1/2020', 126: '6/1/2020', 127: '7/1/2020', 128:
> '8/1/2020', 129: '9/1/2020', 130: '10/1/2020', 131: '11/1/2020',
> 132: '5/1/2019', 133: '6/1/2019', 134: '7/1/2019', 135:
> '8/1/2019', 136: '9/1/2019', 137: '10/1/2019', 138: '11/1/2019',
> 139: '12/1/2019', 140: '1/1/2020', 141: '2/1/2020', 142:
> '3/1/2020', 143: '4/1/2020', 144: '5/1/2020', 145: '6/1/2020',
> 146: '5/1/2019', 147: '6/1/2019', 148: '7/1/2019', 149:
> '8/1/2019', 150: '9/1/2019', 151: '10/1/2019', 152: '11/1/2019',
> 153: '12/1/2019', 154: '1/1/2020', 155: '2/1/2020', 156:
> '3/1/2020', 157: '4/1/2020', 158: '5/1/2020', 159: '6/1/2020',
> 160: '7/1/2020', 161: '8/1/2020', 162: '9/1/2020', 163:
> '10/1/2020', 164: '11/1/2020'}, 'Diff': {0: 160.427898, 1:
> 136.02479, 2: 174.05132, 3: 112.042449, 4: 141.848796, 5: 103.55216800000001, 6: 125.608671, 7: 145.259097, 8: 115.512121, 9: 185.719089, 10: 126.738628, 11: 231.346125, 12: 97.025865,
> 13: 42.85235, 14: 124.66596000000001, 15: 357.99735400000003,
> 16: 490.958654, 17: 204.54779299999998, 18: 287.602512, 19:
> 12.384862, 20: -2.6173490000000004, 21: -5.618695, 22: 3.204252, 23: -25.378199, 24: -10.971694, 25: 4.3095989999999995, 26:
> -18.804298, 27: 2.172899, 28: -13.781198000000002, 29: -41.914787, 30: -54.478880000000004, 31: -12.569756, 32: 5.212716, 33: -8.354158, 34: -24.797067000000002, 35: -15.030110999999998, 36: -6.733579, 37: 20.515503, 38: 16.911068, 39: -3.20907, 40: -6.310559, 41: -28.37903, 42: -2.468109, 43: 17.733227, 44: -7.246225999999999, 45: 9.816127, 46: -27.043909000000003, 47: -14.444707000000001, 48: -16.315007,
> 49: -13.173909, 50: 3.065297, 51: -38.023148, 52:
> -17.331557999999998, 53: -79.566677, 54: -9.81724, 55: -14.70161, 56: 0.7713479999999999, 57: 1.438835, 58: 2.093475, 59: 15.108785999999998, 60: -30.160431, 61: -4.704619999999999,
> 62: -11.864922, 63: 8.38772, 64: -9.329917, 65:
> -13.411660999999999, 66: -23.234234, 67: -30.517737, 68: -4.082799, 69: -0.431963, 70: -17.669752, 71: -29.476111, 72: -39.368627000000004, 73: -10.156292, 74: -5.152712999999999, 75: 14.799759, 76: 24.846001, 77: 1.522557, 78: 29.085378000000002, 79: 17.490861, 80: 9.210548, 81: 5.272687, 82: -34.114118, 83:
> -14.522017000000002, 84: 0.977517, 85: 13.740992000000002, 86: -46.837142, 87: -35.071144, 88: 31.125302, 89: -6.35655, 90: -76.485238, 91: -30.657497999999997, 92: -3.9887309999999996, 93: 10.158892999999999, 94: 13.419127, 95: 5.169942, 96:
> -1.2159360000000001, 97: -14.750085, 98: 2.939289, 99: 7.890761, 100: -17.168810999999998, 101: -4.825164, 102: -23.056132, 103:
> -21.905035, 104: -11.41048, 105: -14.285372, 106: -8.342461, 107: 12.303987, 108: 10.871853999999999, 109: 28.386903000000004,
> 110: -44.330884999999995, 111: 16.979916, 112: 19.051773999999998,
> 113: 10.106931, 114: -5.156007, 115: 4.943011, 116: -3.461768,
> 117: 4.1415239999999995, 118: 6.816288, 119: 2.135951, 120:
> -13.823120000000001, 121: -10.989626, 122: 0.418397, 123: -18.981256, 124: 11.320344, 125: 10.697455999999999, 126: -10.048842, 127: -6.781886999999999, 128: 1.679831, 129: -14.090216, 130: 9.935841, 131: -8.609016, 132: 0.633534, 133: -0.998959, 134: -11.890945, 135: -4.731419, 136: -16.508375, 137: -6.705882000000001, 138: -4.5014970000000005, 139: 2.958923,
> 140: -2.216122, 141: 0.555845, 142: 7.907547999999999, 143:
> -19.098307000000002, 144: 0.5591109999999999, 145: 1.6575990000000003, 146: 141.94400100000001, 147: 120.173635, 148: 142.15808700000002, 149: 185.25196, 150: 145.080391, 151:
> 153.529132, 152: 105.33828700000001, 153: 114.990931, 154: 110.346369, 155: 313.465552, 156: 183.225067, 157: 97.446786, 158: 147.22139199999998, 159: 67.11041800000001, 160: 202.904259,
> 161: 283.446608, 162: 435.956408, 163: 138.866722, 164:
> 265.60073700000004}, 'Percent': {0: 11.10808895, 1: 8.476798147, 2: 9.998946416, 3: 5.8515509020000005, 4: 6.998691053, 5:
> 4.774983768999999, 6: 5.528085128, 7: 6.058015553, 8: 4.542250752, 9: 6.985672892999999, 10: 4.455896349, 11: 7.786734158, 12: 3.0298095369999998, 13: 1.29879184, 14: 3.7299973910000004, 15: 10.32609443, 16: 12.83580027, 17: 4.7394278430000005, 18: 6.362291972, 19: 5.780550638999999, 20: -1.154871997, 21: -2.50814364, 22: 1.4671525440000002, 23: -11.45206837, 24: -5.591372156, 25: 2.3263223280000003, 26: -9.919797042999999, 27: 1.2724943000000002, 28: -7.96914578, 29: -26.336533399999997, 30: -46.46943194, 31: -20.02922699, 32: 10.38652118, 33: -15.07969942, 34: -52.70827375, 35: -67.55468889, 36: -93.27974007, 37: 9.402813387, 38: 7.084646625, 39: -1.255449187, 40: -2.500199374, 41: -11.53189369, 42: -1.133654425, 43: 8.238642233, 44: -3.110265117, 45: 4.348584722, 46: -11.48128956, 47: -6.927792359, 48: -8.407238632, 49: -7.411730457000001, 50: 1.862608372, 51: -22.68204813, 52: -13.37184892, 53: -70.86406019, 54: -30.00925321, 55: -64.20813421, 56: 0.34885011899999996, 57: 0.6484658520000001, 58: 0.937425444, 59: 6.702646341, 60: -12.53946732, 61: -2.236423138, 62:
> -5.769221736, 63: 4.328161823, 64: -4.614618367, 65: -6.954385416, 66: -12.94817733, 67: -19.53685876, 68: -3.248354565, 69: -0.35521689700000003, 70: -14.58219683, 71: -28.47831486, 72: -53.18101653, 73: -29.30350463, 74: -21.02918559, 75: 4.4530310239999995, 76: 7.157090832000001, 77: 0.409291454, 78: 7.786816239, 79: 4.344409532, 80: 2.192481078, 81: 1.2281839890000001, 82: -7.849900319, 83: -3.626278258, 84:
> 0.253279403, 85: 3.5513629, 86: -11.68991986, 87: -9.911988763, 88: 9.764667122999999, 89: -1.8167818040000001, 90: -22.26494689,
> 91: -11.48058055, 92: -1.6874205469999999, 93: 4.371453667, 94:
> 3.693729404, 95: 1.3723787669999998, 96: -0.318404648, 97: -3.8747904839999996, 98: 0.803264717, 99: 2.139245828, 100: -4.557108588999999, 101: -1.3418922629999999, 102: -6.499190071, 103: -6.60391336, 104: -3.68326232, 105: -4.787607361, 106:
> -2.936484265, 107: 4.4619360210000005, 108: 3.7741835389999996, 109: 9.496161526, 110: -13.54370813, 111: 6.000260663, 112:
> 6.351306448, 113: 6.044575831, 114: -2.907847111, 115: 2.87121369, 116: -1.954690645, 117: 2.385137588, 118: 3.834107345, 119: 1.157091192, 120: -7.402630086, 121: -6.355712315, 122: 0.258397668, 123: -11.69241483, 124: 7.896613534, 125: 6.915983594, 126: -6.076406612, 127: -4.366230503, 128: 1.1308641259999999, 129: -9.379480147999999, 130: 7.298593929, 131: -5.89378179, 132: 0.68192986, 133:
> -1.067986868, 134: -12.84984155, 135: -5.86684522, 136: -21.74577714, 137: -11.28804499, 138: -8.54156872, 139: 6.138900437999999, 140: -4.331875908, 141: 1.1357134979999999, 142: 15.97542343, 143: -33.26897683, 144: 1.459536053, 145:
> 4.264846401000001, 146: 9.352928661, 147: 7.241180253, 148: 7.987486665, 149: 9.638910633, 150: 6.885082487999999, 151: 6.816699167, 152: 4.378551195, 153: 4.57927269, 154: 4.201896452, 155: 11.4551689, 156: 6.007535563999999, 157: 3.013993024, 158: 4.4202763439999995, 159: 1.9296724969999999, 160: 5.723796667, 161: 7.562955548, 162: 10.81435501, 163:
> 3.108563316, 164: 5.766284604}, 'Date': {0: '04-01-2019 to 05-01-2019', 1: '05-01-2019 to 06-01-2019', 2: '06-01-2019 to
> 07-01-2019', 3: '07-01-2019 to 08-01-2019', 4: '08-01-2019 to
> 09-01-2019', 5: '09-01-2019 to 10-01-2019', 6: '10-01-2019 to
> 11-01-2019', 7: '11-01-2019 to 12-01-2019', 8: '12-01-2019 to
> 01-01-2020', 9: '01-01-2020 to 02-01-2020', 10: '02-01-2020 to
> 03-01-2020', 11: '03-01-2020 to 04-01-2020', 12: '04-01-2020 to
> 05-01-2020', 13: '05-01-2020 to 06-01-2020', 14: '06-01-2020 to
> 07-01-2020', 15: '07-01-2020 to 08-01-2020', 16: '08-01-2020 to
> 09-01-2020', 17: '09-01-2020 to 10-01-2020', 18: '10-01-2020 to
> 11-01-2020', 19: '04-01-2019 to 05-01-2019', 20: '05-01-2019 to
> 06-01-2019', 21: '06-01-2019 to 07-01-2019', 22: '07-01-2019 to
> 08-01-2019', 23: '08-01-2019 to 09-01-2019', 24: '09-01-2019 to
> 10-01-2019', 25: '10-01-2019 to 11-01-2019', 26: '11-01-2019 to
> 12-01-2019', 27: '12-01-2019 to 01-01-2020', 28: '01-01-2020 to
> 02-01-2020', 29: '02-01-2020 to 03-01-2020', 30: '03-01-2020 to
> 04-01-2020', 31: '04-01-2020 to 05-01-2020', 32: '05-01-2020 to
> 06-01-2020', 33: '06-01-2020 to 07-01-2020', 34: '07-01-2020 to
> 08-01-2020', 35: '08-01-2020 to 09-01-2020', 36: '09-01-2020 to
> 10-01-2020', 37: '04-01-2019 to 05-01-2019', 38: '05-01-2019 to
> 06-01-2019', 39: '06-01-2019 to 07-01-2019', 40: '07-01-2019 to
> 08-01-2019', 41: '08-01-2019 to 09-01-2019', 42: '09-01-2019 to
> 10-01-2019', 43: '10-01-2019 to 11-01-2019', 44: '11-01-2019 to
> 12-01-2019', 45: '12-01-2019 to 01-01-2020', 46: '01-01-2020 to
> 02-01-2020', 47: '02-01-2020 to 03-01-2020', 48: '03-01-2020 to
> 04-01-2020', 49: '04-01-2020 to 05-01-2020', 50: '05-01-2020 to
> 06-01-2020', 51: '06-01-2020 to 07-01-2020', 52: '07-01-2020 to
> 08-01-2020', 53: '08-01-2020 to 09-01-2020', 54: '09-01-2020 to
> 10-01-2020', 55: '10-01-2020 to 11-01-2020', 56: '04-01-2019 to
> 05-01-2019', 57: '05-01-2019 to 06-01-2019', 58: '06-01-2019 to
> 07-01-2019', 59: '07-01-2019 to 08-01-2019', 60: '08-01-2019 to
> 09-01-2019', 61: '09-01-2019 to 10-01-2019', 62: '10-01-2019 to
> 11-01-2019', 63: '11-01-2019 to 12-01-2019', 64: '12-01-2019 to
> 01-01-2020', 65: '01-01-2020 to 02-01-2020', 66: '02-01-2020 to
> 03-01-2020', 67: '03-01-2020 to 04-01-2020', 68: '04-01-2020 to
> 05-01-2020', 69: '05-01-2020 to 06-01-2020', 70: '06-01-2020 to
> 07-01-2020', 71: '07-01-2020 to 08-01-2020', 72: '08-01-2020 to
> 09-01-2020', 73: '09-01-2020 to 10-01-2020', 74: '10-01-2020 to
> 11-01-2020', 75: '04-01-2019 to 05-01-2019', 76: '05-01-2019 to
> 06-01-2019', 77: '06-01-2019 to 07-01-2019', 78: '07-01-2019 to
> 08-01-2019', 79: '08-01-2019 to 09-01-2019', 80: '09-01-2019 to
> 10-01-2019', 81: '10-01-2019 to 11-01-2019', 82: '11-01-2019 to
> 12-01-2019', 83: '12-01-2019 to 01-01-2020', 84: '01-01-2020 to
> 02-01-2020', 85: '02-01-2020 to 03-01-2020', 86: '03-01-2020 to
}}
任何建议表示赞赏
更新
有没有办法让整行在它们上面显示标记点?
我试过这个: mode = 'markers+lines' 但没有得到想要的结果:
**Update, I have figured this out:** 
fig.update_traces(mode='markers+lines')
enter image description here
更新问题:
另外,有没有办法在此处的悬停注释上添加日期和百分比标题?。我正在研究 Plotly 文档。
enter image description here

最佳答案

我已经提出了一个建议,该建议应该完全符合您的要求。下图是由下面的代码示例生成的。该代码使用一个字典,其中包含不同 POD 的不同上限和下限,以及为不同 Pod 设置不同颜色的可能性:

lim = {'IAD': {'lower': 90,'upper': 350, 'color':'yellow'},
'SJCtest': {'lower': 10,'upper': 12, 'color':'green'}}
阴谋
enter image description here
我很想先过滤你的数据框,然后根据它向图中添加新的痕迹。但我的解决方案实际上遍历每个跟踪的每个数据点,并根据上面的字典为点着色。所以不需要任何数据处理。看一看,运行一些测试,然后让我知道它对您的效果如何。
完整代码
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

df = pd.DataFrame({'POD': {0: 'IAD',
1: 'IAD',
2: 'IAD',
3: 'IAD',
4: 'IAD',
5: 'IAD',
6: 'IAD',
7: 'IAD',
8: 'IAD',
9: 'IAD',
10: 'IAD',
11: 'IAD',
12: 'IAD',
13: 'IAD',
14: 'IAD',
15: 'IAD',
16: 'IAD',
17: 'IAD',
18: 'IAD',
19: 'SJCtest',
20: 'SJCtest',
21: 'SJCtest',
22: 'SJCtest',
23: 'SJCtest',
24: 'SJCtest'},
'Start': {0: '4/1/2019',
1: '5/1/2019',
2: '6/1/2019',
3: '7/1/2019',
4: '8/1/2019',
5: '9/1/2019',
6: '10/1/2019',
7: '11/1/2019',
8: '12/1/2019',
9: '1/1/2020',
10: '2/1/2020',
11: '3/1/2020',
12: '4/1/2020',
13: '5/1/2020',
14: '6/1/2020',
15: '7/1/2020',
16: '8/1/2020',
17: '9/1/2020',
18: '10/1/2020',
19: '4/1/2019',
20: '5/1/2019',
21: '6/1/2019',
22: '7/1/2019',
23: '8/1/2019',
24: '9/1/2019'},
'End': {0: '5/1/2019',
1: '6/1/2019',
2: '7/1/2019',
3: '8/1/2019',
4: '9/1/2019',
5: '10/1/2019',
6: '11/1/2019',
7: '12/1/2019',
8: '1/1/2020',
9: '2/1/2020',
10: '3/1/2020',
11: '4/1/2020',
12: '5/1/2020',
13: '6/1/2020',
14: '7/1/2020',
15: '8/1/2020',
16: '9/1/2020',
17: '10/1/2020',
18: '11/1/2020',
19: '5/1/2019',
20: '6/1/2019',
21: '7/1/2019',
22: '8/1/2019',
23: '9/1/2019',
24: '10/1/2019'},
'Diff': {0: 160.4279,
1: 136.0248,
2: 174.0513,
3: 112.0424,
4: 141.8488,
5: 103.5522,
6: 125.6087,
7: 145.2591,
8: 115.5121,
9: 185.7191,
10: 126.7386,
11: 231.3461,
12: 97.02587,
13: 42.85235,
14: 124.666,
15: 357.9974,
16: 490.9587,
17: 204.5478,
18: 287.6025,
19: 12.38486,
20: -2.61735,
21: -5.6187,
22: 3.204252,
23: -25.3782,
24: -10.9717},
'Percent': {0: 11.108089999999999,
1: 8.476797999999999,
2: 9.998946,
3: 5.851551000000001,
4: 6.998691,
5: 4.774984,
6: 5.528085,
7: 6.058016,
8: 4.542251,
9: 6.985672999999999,
10: 4.455896,
11: 7.786734,
12: 3.02981,
13: 1.298792,
14: 3.729997,
15: 10.326089999999999,
16: 12.8358,
17: 4.739428,
18: 6.362292,
19: 5.780551,
20: -1.15487,
21: -2.50814,
22: 1.4671530000000002,
23: -11.4521,
24: -5.5913699999999995},
'Date': {0: '04-01-2019 to 05-01-2019',
1: '05-01-2019 to 06-01-2019',
2: '06-01-2019 to 07-01-2019',
3: '07-01-2019 to 08-01-2019',
4: '08-01-2019 to 09-01-2019',
5: '09-01-2019 to 10-01-2019',
6: '10-01-2019 to 11-01-2019',
7: '11-01-2019 to 12-01-2019',
8: '12-01-2019 to 01-01-2020',
9: '01-01-2020 to 02-01-2020',
10: '02-01-2020 to 03-01-2020',
11: '03-01-2020 to 04-01-2020',
12: '04-01-2020 to 05-01-2020',
13: '05-01-2020 to 06-01-2020',
14: '06-01-2020 to 07-01-2020',
15: '07-01-2020 to 08-01-2020',
16: '08-01-2020 to 09-01-2020',
17: '09-01-2020 to 10-01-2020',
18: '10-01-2020 to 11-01-2020',
19: '04-01-2019 to 05-01-2019',
20: '05-01-2019 to 06-01-2019',
21: '06-01-2019 to 07-01-2019',
22: '07-01-2019 to 08-01-2019',
23: '08-01-2019 to 09-01-2019',
24: '09-01-2019 to 10-01-2019'}})

fig = px.line(df, x="Date", y="Diff", color = 'POD')

import plotly.graph_objects as go
included = 0

lim = {'IAD': {'lower': 90,'upper': 350, 'color':'yellow'},
'SJCtest': {'lower': 10,'upper': 12, 'color':'green'}}

for i, d in enumerate(fig.data):
for j, y in enumerate(d.y):
if y < lim[d.name]['lower'] or y > lim[d.name]['upper']:

if j == 0:
fig.add_traces(go.Scatter(x=[fig.data[i]['x'][j]],
y=[fig.data[i]['y'][j]],
mode = 'markers',
marker = dict(color=lim[d.name]['color']),
name = d.name + ' threshold',
legendgroup = d.name + ' threshold'))
included = included + 1
else:
fig.add_traces(go.Scatter(x=[fig.data[i]['x'][j-1], fig.data[i]['x'][j]],
y=[fig.data[i]['y'][j-1], fig.data[i]['y'][j]],
mode = 'lines',
# marker = dict(color='yellow'),
line = dict(width = 6, color = lim[d.name]['color']),
name = d.name + ' threshold',
legendgroup = d.name + ' threshold',
showlegend = False if included > 0 else True,
))
included = included + 1

fig.show()

关于python - Plotly:如何在指定阈值的折线图上显示不同的颜色段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64760484/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com