如何修复量的范围在第二Y轴

问题描述 投票:1回答:1

我完整的代码:

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()

我有一个量价数据框,我用下面的语法在第二y轴绘制Volume

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

python python-3.x dataframe matplotlib
1个回答
0
投票

既然你想控制/修改辅助y轴刻度标签,我会用稍微不同的方式绘制:一次绘制一列。一旦绘制辅助y轴数据,就可以通过提供所需的刻度标记的列表覆盖缺省刻度标记。根据您所提供的数据框,下面是做这件事

# 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

© www.soinside.com 2019 - 2024. All rights reserved.