简体   繁体   English

如何固定第二个y轴的音量范围

[英]How to fix the range of Volume in 2nd y-Axis

My full code: 我的完整代码:

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

I have a price-volume DataFrame and I am using below syntax to plot Volume in the second y-axis. 我有一个价格量的DataFrame,并且正在使用以下语法在第二个y轴上绘制Volume

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

My data in Volume column ranges from 0-500. 我在“ Volume列中的数据范围是0-500。 When I am plotting it using the above syntax, the second y-axis takes the maximum and minimum of the Volume and adjust the range automatically. 当我使用以上语法进行绘制时,第二个y轴将使用Volume的最大值和最小值并自动调整范围。

I need to fix the bracket on second y-axis while plotting. 在绘制时,我需要将支架固定在第二个y轴上。 I want the range of Volume as 0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500 我希望Volume范围为0、50、100、150、200、250、300、350、400、450、500

Sample Data: 样本数据:

       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

Sample image for more clarity 样本图片更加清晰

在此处输入图片说明

Since you want to control/modify the secondary y-axis tick labels, I would use slightly different approach to plot: plotting one column at a time. 由于您要控制/修改次要y轴刻度标签,因此我将使用略有不同的绘制方法:一次绘制一列。 Once you plot the secondary y-axis data, you can override the default tick labels by providing a list of desired tick labels. 绘制辅助y轴数据后,您可以通过提供所需的刻度标签列表来覆盖默认刻度标签。 Based on the DataFrame you provided, following is one way to do it 根据您提供的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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM