2016-04-03 25 views
0

Grafik üzerinde bir şekil çizmeye çalışıyorum. İşte benim kodu ... points = ... ve line = ... ile başlayanMatplotlib Bir grafik üzerinde çizim grafikleri

def graphData(self, stock): 
    stock_file = Company.objects.get(ticker_name=stock) 
    stock_file = stock_file.data 
    stock_file = stock_file.replace("[","").replace("]","").replace("'","") 
    stock_file = re.sub("(([^,]*,){5}[^,]*),\s*","\\1\n",stock_file) 
    file_name = "/home/philip/PycharmProjects/stockmarket/static/stock_data/data_file.txt" 
    file = open(file_name, "w") 

    points = [[2, 4], [2, 8], [4, 6], [6, 8]] 
    line = plt.Polygon(points, closed=None, fill=None, edgecolor='r') 

    file.write(stock_file) 
    file.close() 
    date, closep, highp, lowp, openp, volume = np.loadtxt(file_name, delimiter=",", unpack=True, converters={ 0: date_converter }) 
    fig = plt.figure() 
    ax1 = plt.subplot(1,1,1) 
    ax1.plot(date, openp) 
    ax1.plot(date, highp) 
    ax1.plot(date, lowp) 
    ax1.plot(date, closep) 
    ax1.grid(True) 
    ax1.xaxis.set_major_locator(mticker.MaxNLocator(10)) 
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y')) 
    for label in ax1.xaxis.get_ticklabels(): 
     label.set_rotation(90) 
    plt.subplots_adjust(left=.10, bottom=.22, right=.93, top=.95) 
    plt.xlabel('Date') 
    plt.ylabel('Stock Price') 
    plt.suptitle(stock+': Stock Price') 
    plt.show() 
    plt.savefig("/home/philip/PycharmProjects/stockmarket/static/graph.svg") 

iki ayrı hat denemek ve şekil çizmek için eklenen kod olmasıdır. Bu neden çalışmıyor?

cevap

0

Çağrı ax.add_patch(line) eksene Polygon, ax eklemek için:

import numpy as np 
import matplotlib.pyplot as plt 
np.random.seed(2016) 

points = [(2, 4), (2, 8), (4, 6), (6, 8)] 
line = plt.Polygon(points, closed=None, fill=None, edgecolor='r') 

fig = plt.figure() 
ax = plt.subplot(1,1,1) 
ax.add_patch(line) 

date = np.linspace(0, 7, 100) 
openp = (3*np.sin(8*date)/date)+6 
ax.plot(date, openp) 
ax.set(xlim=(1,7), ylim=(3,9)) 
plt.show() 

enter image description here

+0

Hmm. Hala çalışmıyor gibi görünüyor. Sistem hatası olup olmadığını doğrulamak için bunu kodumla deneyebilir misiniz? –

+0

Tamam, şimdi bunu çizmek için elimden geleni yaptım, ancak diğer veriler yerine bunun yerine onu çiziyor. –

+0

"Puanlar" koordinatları ile "tarih" ve hisse senedi fiyatı değerleri arasındaki ilişki nedir? Örneğin, '(2, 4)' kelimesi grafik üzerinde yer almalı, eğer tarih '2000-1-1' ile '2016-1-1' arasında değişiyorsa? – unutbu

İlgili konular