试图弄清楚在python中绘制数字线上点的最佳方法是什么.基本上尝试制作类似于下图的内容:

我一直在尝试使用Matplotlib来做到这一点,但似乎无法弄明白.有人知道我可以使用的包裹或任何东西吗?
使用正常工作的matplotlib设置图形(见下图),但是当我尝试添加图例时,我收到以下错误: UserWarning: No labeled objects found. Use label='...' kwarg on indivial plots.
这是我用来在图例中定义我想要的线条并绘制图例的代码:
#Moving average labels
smaLabel1 = str(SMA1)+'d SMA'
smaLabel2 = str(SMA2)+'d SMA'
smaLabel3 = str(SMA3)+'d SMA'
#Add SMAs to chart
ax1.plot(ind, avg1, '#5998ff', label=smaLabel1, linewidth=1)
ax1.plot(ind, avg2, '#ffbb82', label=smaLabel2, linewidth=1)
ax1.plot(ind, avg3, '#d689c4', label=smaLabel3, linewidth=1)
""" End SMA additions """
#Add legend
plt.legend()
Run Code Online (Sandbox Code Playgroud)
我检查了smaLabel变量,并且都保存了正确的字符串.有人知道为什么标签没有注册?

试图比较Python中的两个日期.当我运行type()检查时,两者都显示为
<type 'datetime.date'>
然而,当我对两个日期进行简单检查时,我收到以下错误:
AttributeError: 'datetime.date' object has no attribute 'date'
任何人都可以告诉我这里我做错了什么.代码如下:
d_today = date.today()
oldestDate = d_today - BDay(750)
avgArray2 = [x for x in avgArray if x[0] >= oldestDate.date()]
print type(oldestDate.date())
print type(avgArray[0][0])
Run Code Online (Sandbox Code Playgroud)
并输出:
<type 'datetime.date'>
<type 'datetime.date'>
avgArray2 = [x for x in avgArray if x[0].date() >= oldestDate.date()]
AttributeError: 'datetime.date' object has no attribute 'date'
Run Code Online (Sandbox Code Playgroud)
有关如何构建"avgArray"的完整代码:
d_today = date.today()
d_ref = d_today - BDay(66)
lastDateData = dates[0]
avgArray = []
while (d_ref >= lastDateData):
avg_data = [x …Run Code Online (Sandbox Code Playgroud) 创建了一个图例并根据需要格式化文本,但无法弄清楚如何删除“破折号”行以便只显示文本。这是我现在得到的(注意该行是如何穿过右对齐的文本):

#Add legend
leg = ax1.legend(bbox_to_anchor=(0.03, 1.05), prop={'size':8})
leg.get_frame().set_alpha(0)
legText = pylab.gca().get_legend().get_texts()
#Format legend text
legText[0].set_color('#5998ff')
legText[1].set_color('#ffbb82')
legText[2].set_color('#d689c4')
for text in legText:
text.set_ha('right')
Run Code Online (Sandbox Code Playgroud) 我有一个脚本(见下文),它在Windows中完美运行,我正在尝试迁移到Ubuntu环境.我已经设置了完全相同的PostgreSQL数据库,具有完全相同的表和用户名等.但是,当我尝试在Ubunu中运行脚本时,它在解析"with"语句时失败.
这是"with"声明:
with con:
cur = con.cursor()
cur.executemany(final_str, symbols)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
INSERT INTO symbol (ticker, instrument, name, sector, currency, created_date, last_updated_date) VALUES (%s, %s, %s, %s, %s, %s, %s) 502
Traceback (most recent call last):
File "loadSPX.py", line 60, in <module>
insert_snp500_symbols(symbols)
File "loadSPX.py", line 54, in insert_snp500_symbols
with con:
AttributeError: __exit__
Run Code Online (Sandbox Code Playgroud)
但是,如果我删除"with"并将其更改为以下内容,则可以完美地运行:
cur = con.cursor()
cur.executemany(final_str, symbols)
con.commit()
Run Code Online (Sandbox Code Playgroud)
是什么原因引起了这个?以下是完整的脚本:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import lxml.html
import psycopg2 as mdb
import psycopg2.extras
from math import ceil
def …Run Code Online (Sandbox Code Playgroud) python ×5
matplotlib ×3
charts ×2
date ×1
datetime ×1
graph ×1
legend ×1
postgresql ×1
ubuntu ×1
windows ×1