如何在辅助 y 上用一条线和另一条线创建面积图

Posted

技术标签:

【中文标题】如何在辅助 y 上用一条线和另一条线创建面积图【英文标题】:How to create an area plot with a line and another line on a secondary y 【发布时间】:2021-12-23 20:04:24 【问题描述】:

我有 4 个列表要绘制在同一个图表上。 4 个中的 3 个共享相同的比例,而 x 轴值在所有 4 个中都相同。我想为每个 x 日期值绘制图并将其显示在图上。

这些是我的清单:

date_list = [datetime.date(2008, 1, 1), datetime.date(2008, 2, 1), datetime.date(2008, 3, 1), datetime.date(2008, 4, 1), datetime.date(2008, 5, 1), datetime.date(2008, 7, 1), datetime.date(2009, 7, 1), datetime.date(2010, 7, 1), datetime.date(2010, 8, 1), datetime.date(2010, 9, 1), datetime.date(2010, 10, 1), datetime.date(2010, 11, 1), datetime.date(2010, 12, 1), datetime.date(2011, 1, 1), datetime.date(2011, 2, 1), datetime.date(2011, 3, 1), datetime.date(2011, 4, 1), datetime.date(2011, 5, 1), datetime.date(2011, 6, 1), datetime.date(2011, 7, 1), datetime.date(2011, 10, 1), datetime.date(2011, 11, 1), datetime.date(2011, 12, 1), datetime.date(2012, 1, 1), datetime.date(2012, 2, 1), datetime.date(2012, 10, 1), datetime.date(2012, 11, 1), datetime.date(2012, 12, 1), datetime.date(2013, 1, 1), datetime.date(2013, 2, 1), datetime.date(2013, 8, 1), datetime.date(2013, 11, 1), datetime.date(2013, 12, 1), datetime.date(2014, 1, 1), datetime.date(2014, 2, 1), datetime.date(2014, 3, 1), datetime.date(2014, 4, 1), datetime.date(2014, 5, 1), datetime.date(2014, 6, 1), datetime.date(2014, 7, 1), datetime.date(2015, 2, 1), datetime.date(2015, 3, 1), datetime.date(2015, 4, 1), datetime.date(2015, 5, 1), datetime.date(2015, 6, 1), datetime.date(2015, 7, 1), datetime.date(2015, 8, 1), datetime.date(2015, 9, 1), datetime.date(2015, 10, 1), datetime.date(2015, 11, 1), datetime.date(2015, 12, 1), datetime.date(2016, 1, 1), datetime.date(2016, 2, 1), datetime.date(2016, 3, 1), datetime.date(2016, 4, 1), datetime.date(2016, 5, 1), datetime.date(2016, 6, 1), datetime.date(2016, 7, 1), datetime.date(2016, 8, 1), datetime.date(2016, 9, 1), datetime.date(2016, 10, 1), datetime.date(2016, 11, 1), datetime.date(2016, 12, 1), datetime.date(2017, 1, 1), datetime.date(2017, 2, 1), datetime.date(2017, 3, 1), datetime.date(2017, 4, 1), datetime.date(2017, 5, 1), datetime.date(2017, 6, 1), datetime.date(2017, 7, 1), datetime.date(2017, 8, 1), datetime.date(2017, 9, 1), datetime.date(2017, 10, 1), datetime.date(2017, 11, 1), datetime.date(2017, 12, 1), datetime.date(2018, 1, 1), datetime.date(2018, 2, 1), datetime.date(2018, 3, 1), datetime.date(2018, 4, 1), datetime.date(2018, 5, 1), datetime.date(2018, 6, 1), datetime.date(2018, 7, 1), datetime.date(2018, 8, 1), datetime.date(2018, 9, 1), datetime.date(2018, 10, 1), datetime.date(2018, 11, 1), datetime.date(2018, 12, 1), datetime.date(2019, 1, 1), datetime.date(2019, 2, 1), datetime.date(2019, 3, 1), datetime.date(2019, 4, 1), datetime.date(2019, 5, 1), datetime.date(2019, 6, 1), datetime.date(2019, 7, 1), datetime.date(2019, 8, 1), datetime.date(2019, 9, 1), datetime.date(2019, 10, 1), datetime.date(2019, 11, 1), datetime.date(2019, 12, 1), datetime.date(2020, 1, 1), datetime.date(2020, 2, 1), datetime.date(2020, 3, 1), datetime.date(2020, 4, 1), datetime.date(2020, 5, 1), datetime.date(2020, 6, 1), datetime.date(2020, 7, 1), datetime.date(2020, 8, 1), datetime.date(2020, 9, 1), datetime.date(2020, 10, 1), datetime.date(2020, 11, 1), datetime.date(2020, 12, 1)]

w = [318.5, 1195.6, 1174.1, 317.2, 1453.9, 1274.2, 499.9, 690.2, 247.9, 623.9, 719.8, 462.1, 591.7, 662.4, 530.6, 1225.0, 1190.5, 1474.0, 734.8, 650.2, 701.0, 1028.7, 348.8, 273.8, 1028.8, 1244.0, 1546.9, 689.0, 415.3, 1406.5, 623.1, 826.6, 1106.5, 1228.0, 1474.2, 625.5, 670.0, 1459.2, 1294.8, 432.5, 1472.4, 1124.2, 584.3, 1329.5, 219.5, 1156.4, 1524.4, 1039.2, 790.7, 407.7, 907.4, 885.7, 858.0, 1133.9, 469.6, 801.1, 995.1, 1271.9, 821.8, 250.3, 289.2, 1478.2, 1417.8, 1325.8, 853.3, 415.7, 1393.9, 829.6, 390.2, 985.1, 581.1, 770.5, 588.4, 1522.3, 580.1, 800.1, 558.1, 1500.1, 418.6, 1520.2, 1414.8, 648.9, 907.5, 1205.4, 773.7, 210.5, 1443.9, 956.2, 488.0, 678.8, 648.8, 1018.5, 1046.2, 1160.1, 479.4, 891.7, 1400.7, 696.0, 555.5, 614.1, 1451.7, 1038.4, 261.7, 718.2, 1186.9, 498.8, 793.4, 1194.3, 929.0, 304.8, 1029.4]

x = [3193.1, 3934.2, 2775.9, 4295.0, 2627.2, 3803.7, 4412.7, 3954.2, 2756.1, 4167.6, 3752.2, 4460.3, 5452.4, 5062.5, 4981.4, 2971.5, 3721.2, 2568.0, 3676.2, 4690.1, 3312.4, 5391.4, 3606.8, 3967.0, 4424.4, 2606.8, 5325.3, 3492.1, 3490.6, 4944.7, 3538.7, 4388.4, 4570.3, 4595.6, 2628.3, 4354.3, 3675.1, 2569.8, 3603.6, 3583.0, 4501.8, 2747.1, 4502.0, 3120.8, 4045.2, 4222.6, 4582.9, 4410.9, 4816.2, 4420.7, 3813.9, 2514.1, 2576.5, 4330.4, 3852.9, 3018.0, 3870.0, 3880.7, 5403.7, 4598.0, 4570.6, 4372.3, 4897.3, 4957.4, 4068.6, 2604.1, 4414.7, 2756.3, 3948.1, 2504.6, 3844.0, 3109.8, 4241.5, 2630.4, 3626.9, 2772.9, 3346.9, 4940.6, 4686.7, 2555.1, 4010.9, 4433.4, 5442.0, 2667.9, 4936.7, 4960.8, 4887.6, 4809.1, 2954.1, 3354.5, 5401.4, 3825.5, 2904.6, 4961.5, 3501.3, 2762.0, 2582.7, 3236.6, 5377.4, 4200.7, 3845.9, 4401.4, 3260.5, 4078.6, 3221.2, 3801.5, 4122.8, 5317.4, 3952.7, 3376.1, 3322.6]

y = [5111.4, 1024.2, 626.9, 4169.6, 4596.8, 2673.1, 512.4, 3669.0, 5283.4, 543.9, 3582.7, 1202.4, 769.0, 4277.2, 2170.2, 1685.3, 3798.6, 2626.8, 3008.5, 3336.4, 5360.8, 3765.7, 4313.1, 4678.8, 2354.3, 2819.1, 622.6, 185.3, 5129.2, 4907.7, 816.5, 1432.7, 3746.6, 1419.7, 4330.9, 3877.5, 2025.9, 4570.3, 751.0, 2299.0, 1157.9, 1450.8, 3930.4, 3574.1, 2871.9, 4729.0, 2880.7, 4985.4, 4422.2, 4480.3, 2887.5, 1960.5, 2367.5, 1876.1, 3832.4, 4072.7, 1827.8, 1780.8, 3660.0, 2372.4, 2353.7, 5203.9, 1124.5, 3463.2, 3369.3, 1745.2, 2427.7, 1306.0, 1222.2, 1845.8, 2735.2, 5111.0, 1550.0, 4889.4, 4862.7, 4989.6, 3881.9, 3179.3, 1179.3, 1328.5, 2661.7, 5187.1, 4981.0, 1547.9, 348.5, 2981.7, 5031.3, 3711.4, 2548.3, 2953.6, 455.6, 3282.8, 2021.3, 1494.2, 1731.6, 536.0, 2025.0, 419.9, 976.8, 4353.2, 4578.3, 359.8, 3963.8, 1139.6, 4463.0, 5193.6, 3992.5, 2992.7, 2968.7, 4641.9, 4622.6]

z = [0.0, 45.0, 20.0, 10.0, 10.0, 40.0, 25.0, 35.0, 20.0, 40.0, 15.0, 5.0, 10.0, 25.0, 20.0, 40.0, 45.0, 5.0, 15.0, 15.0, 0.0, 30.0, 40.0, 40.0, 10.0, 15.0, 10.0, 45.0, 20.0, 25.0, 30.0, 10.0, 10.0, 0.0, 10.0, 15.0, 25.0, 20.0, 0.0, 30.0, 40.0, 15.0, 45.0, 45.0, 40.0, 40.0, 40.0, 45.0, 15.0, 0.0, 30.0, 45.0, 0.0, 40.0, 0.0, 40.0, 5.0, 30.0, 20.0, 5.0, 10.0, 35.0, 20.0, 45.0, 10.0, 0.0, 20.0, 25.0, 30.0, 25.0, 40.0, 10.0, 15.0, 20.0, 0.0, 10.0, 5.0, 45.0, 20.0, 10.0, 45.0, 5.0, 45.0, 5.0, 15.0, 0.0, 40.0, 35.0, 15.0, 20.0, 10.0, 25.0, 10.0, 20.0, 5.0, 45.0, 40.0, 10.0, 10.0, 5.0, 35.0, 30.0, 10.0, 45.0, 15.0, 5.0, 30.0, 45.0, 10.0, 35.0, 15.0]

列表 w、x 和 y 的比例相同,而 z 的比例从 0 到 100 不同。附上一张我试图重现的图片。

这是我迄今为止的尝试:

import datetime

import matplotlib.pyplot as plt

fig,ax=plt.subplots(figsize = (100, 50))

ax.stackplot(date_list, y, x,colors = ["r", "skyblue"],  alpha=0.8)
ax.plot(date_list, w,lw = 2.5, color = "g")
ax.set_yticklabels(y, fontsize=50)
ax.set_xticklabels(date_list, fontsize = 50)


ax2=ax.twinx()
ax2.plot(date_list, z, color = 'b')

ax.legend(loc=2, fontsize='large')
ax2.legend(loc=3, fontsize='large')
plt.show()

z = 蓝色,w = 绿色,x = 红色,y = 灰色

【问题讨论】:

希望答案是有帮助的。彻底回答问题很费时间。如果您的问题已解决,请接受解决方案 位于答案左上角的 ▲/▼ 箭头下方。如果出现更好的解决方案,则可以接受新的解决方案。如果您的声望超过 15,您还可以使用 ▲/▼ 箭头对答案的有用性进行投票。 如果解决方案无法回答问题,请发表评论。 What should I do when someone answers my question?。谢谢。 【参考方案1】: 完成此操作的最简单方法可能是将数据加载到pandas.DataFrame,然后使用pandas.DataFrame.plot 进行绘图 如果您想将所有现有日期作为 x 刻度,则将 p3 替换为以下内容,并增加 figsize=,但它可能会过度拥挤 x 轴。 df.plot(y='z', ax=ax, secondary_y=True, color='blue', xticks=df.index, rot=90)
import pandas as pd

# create dataframe
df = pd.DataFrame('w': w, 'x': x, 'y': y, 'z': z, index=date_list)

# plot the areas
ax = df.plot(kind='area', y=['x', 'y'], figsize=(15, 7), color=['red', 'lightsteelblue'])

# plot the line on the same y
p2 = df.plot(y='w', ax=ax, color='green')

# plot the line on a secondary y
p3 = df.plot(y='z', ax=ax, secondary_y=True, color='blue')

【讨论】:

以上是关于如何在辅助 y 上用一条线和另一条线创建面积图的主要内容,如果未能解决你的问题,请参考以下文章

如何在matlab中以方位角画一条线?

如何判断一个点是在一条线的右侧还是左侧

使用qwt画图时如何获得一条线的最右边的宽度

对燃尽图的理解

如何使用线性感知器中的权重向量绘制一条线? [关闭]

如何在 R Plotly 中用颜色向量绘制一条线