gpt4 book ai didi

python - 如何固定第二个 y 轴的体积范围

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:36 24 4
gpt4 key购买 nike

我的完整代码:

import matplotlib.pyplot as plt
plt.style.use('ggplot')
import pandas as pd
import numpy as np

path = 'C:\\MyPath\\File1.txt'
dff = pd.read_csv(path, sep=",")
dff.columns = ['Date','Time','Price','Volume']
dff.plot('Time', ['Price', 'Volume'], secondary_y='Volume')
plt.show()

我有一个价格-成交量 DataFrame,我使用以下语法在第二个 y 轴上绘制成交量

dff.plot('Time', ['Price', 'Volume'], secondary_y='Volume')

我在Volume列中的数据范围为0-500。当我使用上述语法绘制它时,第二个 y 轴采用 Volume 的最大值和最小值并自动调整范围。

我需要在绘图时将支架固定在第二个 y 轴上。我想要 Volume 的范围为 0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500

示例数据:

       Date        Time      Price     Volume
72612 31/01/2019 15:26:58 135.85 100
72613 31/01/2019 15:27:02 135.90 110
72614 31/01/2019 15:27:03 135.95 140
72615 31/01/2019 15:27:07 135.95 100
72616 31/01/2019 15:27:10 135.85 60
72617 31/01/2019 15:27:11 135.90 150
72618 31/01/2019 15:27:13 135.95 100
72619 31/01/2019 15:27:15 135.95 100
72620 31/01/2019 15:27:18 135.95 30
72621 31/01/2019 15:27:22 135.95 10

示例图片更清晰

enter image description here

最佳答案

由于您想要控制/修改辅助 y 轴刻度标签,因此我将使用略有不同的方法来绘制:一次绘制一列。绘制辅助 y 轴数据后,您可以通过提供所需刻度标签列表来覆盖默认刻度标签。根据您提供的 DataFrame,以下是一种方法

# Read in your dataframe here

ax = dff.plot('Time','Price')
ax2 = dff.plot('Time','Volume', ax=ax, secondary_y=True)

y_ticks = range(0, 501, 50)
ax2.set_yticklabels(y_ticks)
plt.show()

enter image description here

关于python - 如何固定第二个 y 轴的体积范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54495784/

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