Python 使用颜色列Matplotlib绘制矩形
我在画OHLC蜡烛的颜色。这是使用以下可用代码实现的: 但当我在代码中尝试时,出现了一个问题。我会告诉你这里的情况Python 使用颜色列Matplotlib绘制矩形,python,python-3.x,pandas,matplotlib,plotly,Python,Python 3.x,Pandas,Matplotlib,Plotly,我在画OHLC蜡烛的颜色。这是使用以下可用代码实现的: 但当我在代码中尝试时,出现了一个问题。我会告诉你这里的情况 dataset_train = pd.read_csv('EURUSD_M1TechnicalIndicators.csv',usecols=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]) dataset_train.head(10) dataset_test = pd.read_csv('Test_EURUSD_M1TechnicalIndicato
dataset_train = pd.read_csv('EURUSD_M1TechnicalIndicators.csv',usecols=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
dataset_train.head(10)
dataset_test = pd.read_csv('Test_EURUSD_M1TechnicalIndicators.csv',usecols=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],nrows=50)
dataset_test.head(10)
train = dataset_train.filter(['open','high','low','close','closeTarget',"k","d","atr","macdmain","macdsgnal","bbup","bbmid","bblow"], axis=1)
x = train.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
datasetTrain = pd.DataFrame(x_scaled)
datasetTrain.columns = ['open','high','low','close','closeTarget',"k","d","atr","macdmain","macdsgnal","bbup","bbmid","bblow"]
datasetTrain.head(10)
test = dataset_test.filter(['open','high','low','close','closeTarget',"k","d","atr","macdmain","macdsgnal","bbup","bbmid","bblow"], axis=1)
x = test.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
datasetTest = pd.DataFrame(x_scaled)
datasetTest.columns = ['open','high','low','close','closeTarget',"k","d","atr","macdmain","macdsgnal","bbup","bbmid","bblow"]
datasetTest.head(10)
plt.figure(figsize=(25,5))
plt.plot(xTrain[:,3])
plt.title('Train (' +str(len(xTrain))+' data points)')
plt.show()
plt.figure(figsize=(10,3))
plt.plot(xTest[:,0])
plt.title('Test (' +str(len(xTest))+' data points)')
plt.show()
到目前为止,产量为:
然后我试了一下:
def draw_rects(ax, quotes, width=5., height=1., yloc=1., colorup='g',
colordown='r', edgecolor='k', alpha=1.0):
OFFSET = width / 2.0
patches = []
for q in quotes:
t, open, close, high, low = q[:5]
if close > open:
color = colorup
else:
color = colordown
rect = Rectangle(
xy=(t - OFFSET, yloc),
width=width,
height=height,
facecolor=color,
edgecolor=edgecolor,
)
rect.set_alpha(alpha)
patches.append(rect)
ax.add_patch(rect)
ax.autoscale_view()
return patches
fig, ax = plt.subplots(1,1)
quotes = xTest
p1 = draw_rects(ax, xTrain, yloc=1)
p2 = draw_rects(ax, xTest, yloc=4)
labels = [item.get_text() for item in ax.get_yticklabels()]
labels[2] = 'Train'
labels[8] = 'Test'
ax.set_yticklabels(labels)
plt.show()
输出:
我正期待着蜡烛的颜色被展示出来。所以上面的过程对我来说不起作用。因此,我在数据集中创建了另一列作为color和shiftedcolor
现在我正在考虑使用矩形以烛光方式展示数据集中的颜色数据。请帮我做那件事
这是我使用的数据集>P>我没有真正地解决你的问题的复杂性,绘制一些矩形(没有冒犯,如果我忽略了重要的东西,请纠正我,换句话说:TL;Dr.…)
但作为一个例子,我将如何绘制这两行(假设作为一种不同的方法来开始讨论它是否值得…):
导致:
编辑:
同样的事情也适用于您的数据文件:
(我看到你也想要黑色表示相等的值,所以也添加了这个)
哇,这是我迄今为止得到的一个非常好的答案。你能帮我把它们贴上标签吗?请贴哪种标签?如果您想获得上面发布的x记号标签,可以设置“x=np.arange(-2,4)”。或y轴标签的“plt.yticks([1,4],“Train”,“Test'])。或者你想给标记本身加上标签?很明显,我指的是轴标签。我想已经完成了。还有一件事,你能告诉我,如果我想增加两个矩形之间的间距,那么我必须做什么吗?你可以改变矩形的纵横比,它是在顶点中定义的。我添加了两个参数rx
和ry
,以便您可以轻松调整。谢谢您的帮助。我正在查看。通过查看绘图,现在知道您的文件有30202行-难道这个绘图不是很好,只是因为数千个(可能重叠)矩形而难以读取吗?
x = np.arange(10)
y = np.ones(10)
bools = np.random.randint(0, 2, 10).astype(bool)
colors = np.array(bools, str)
colors[bools] = 'g'
colors[~bools] = 'r'
rx = 1
ry = 2
rect = [(-rx, -ry), (rx, -ry), (rx, ry), (-rx, ry)]
plt.figure()
plt.scatter(x, y, facecolor=colors, verts=rect, s=1000)
plt.scatter(x, y+3, facecolor=colors[::-1], verts=rect, s=1000)
plt.ylim(0, 5)
plt.yticks([1, 4], ['Train', 'Test'])
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dataset_train = pd.read_csv('EURUSD_M1TechnicalIndicators.txt', usecols=['open', 'close'])
dataset_test = pd.read_csv('Test_EURUSD_M1TechnicalIndicators.csv', usecols=['open', 'close'])
gt_train = (dataset_train.open - dataset_train.close) > 0
gt_test = (dataset_test.open - dataset_test.close) > 0
eq_train = (dataset_train.open - dataset_train.close) == 0
eq_test = (dataset_test.open - dataset_test.close) == 0
y_train = np.ones(len(gt_train))
y_test = np.ones(len(gt_test))
colors_train = np.array(gt_train, str)
colors_test = np.array(gt_test, str)
colors_train[gt_train] = 'g'
colors_train[~gt_train] = 'r'
colors_train[eq_train] = 'k'
colors_test[gt_test] = 'g'
colors_test[~gt_test] = 'r'
colors_test[eq_test] = 'k'
rx = .2
ry = 2
rect = [(-rx, -ry), (rx, -ry), (rx, ry), (-rx, ry)]
plt.figure()
plt.scatter(np.arange(y_train.size), y_train, facecolor=colors_train, verts=rect, s=1000)
plt.scatter(np.arange(y_test.size), y_test+3, facecolor=colors_test, verts=rect, s=1000)
plt.ylim(0, 5)
plt.yticks([1, 4], ['Train', 'Test'])