如何在matplotlib中绘制多个条形图,当我试图多次调用条形函数时,它们重叠,如下图所示,红色的最高值只能看到.如何在x轴上绘制带有日期的多个条形图?
到目前为止,我试过这个:
import matplotlib.pyplot as plt
import datetime
x = [
datetime.datetime(2011, 1, 4, 0, 0),
datetime.datetime(2011, 1, 5, 0, 0),
datetime.datetime(2011, 1, 6, 0, 0)
]
y = [4, 9, 2]
z = [1, 2, 3]
k = [11, 12, 13]
ax = plt.subplot(111)
ax.bar(x, y, width=0.5, color='b', align='center')
ax.bar(x, z, width=0.5, color='g', align='center')
ax.bar(x, k, width=0.5, color='r', align='center')
ax.xaxis_date()
plt.show()
Run Code Online (Sandbox Code Playgroud)
我懂了:
结果应该是这样的,但是日期在x轴上,并且柱子彼此相邻:
我正在尝试为我正在创建的文件命名.我只是想知道Android中的最大文件名长度是多少?
是否有文件名规范?我可以用这样的字符-
或>
?
在views.py中
当我尝试从其他人访问全局变量时def
:
def start(request):
global num
num=5
return HttpResponse("num= %d" %num) # returns 5 no problem....
def other(request):
num=num+1
return HttpResponse("num= %d" %num)
Run Code Online (Sandbox Code Playgroud)
def other
不回6,但它应该是6对吗?如何在视图中全局访问变量?
我是django的新手,我想获得名称字段包含"John"的id.下面的代码片段运行得非常好,但是,
在view.py中
all_ids=Employee.objects.filter(name__contains = 'John').values('id')
return HttpResponse(" All id= %d " %all_ids)
Run Code Online (Sandbox Code Playgroud)
它返回:
All id=[{'id': 1},{'id':2} , so on '...(remaining elements truncated)...']
Run Code Online (Sandbox Code Playgroud)
显示项目存在限制.那么,我怎样才能摆脱这种局限并(其余元素被截断)?有没有更好的方法在查询中获取字段的所有值而不截断?
date1 = datetime.datetime(2012, 10, 10, 10, 15, 44)
date2 = datetime.datetime(2012, 10, 17, 8, 45, 38)
roundedA = date2.replace(second = 0, microsecond = 0)
print roundedA
roundedB = date1.replace(second = 0, microsecond = 0)
print roundedB
minutes = (roundedA - roundedB).min
print minutes
Run Code Online (Sandbox Code Playgroud)
结果是:
-999999999 days, 0:00:00
Run Code Online (Sandbox Code Playgroud)
我想计算2个不同的日期差异.我在上面减去了,但它没有给我我想要的东西.如何减去两个日期并在几分钟或几小时内得到结果.
可能的重复:
Django 获取模型的字段
我有下一个模型:
class People(models.Model):
name = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
Run Code Online (Sandbox Code Playgroud)
我想遍历 People 表中的所有项目。
在views.py中
- 当我尝试这个时:
for each in People().objects.all():
name=each.name
Run Code Online (Sandbox Code Playgroud)
我收到那个错误:
Manager isn't accessible via People instances
Run Code Online (Sandbox Code Playgroud)
- 当我尝试这个时:
for each in People():
name=each.name
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
'People' object is not iterable
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题以及如何遍历 People 表中的所有项目?
我从def start方法开始,它调用go_adder在adder.html中添加5次num值,直到num等于5.之后,adder方法应该返回ready = 1
在views.py中
def start(request):
num=0
ready_or_not=go_adder(num)
return HttpResponse("Ready: %s "%str(ready_or_not))
def go_adder(num):
ready=0
if num<5:
return render_to_response('adder.html',{'num':num})
elif num==5:
ready=1
return ready
def check_post(request,num):
if request.method == 'POST':
num+=1
return adder(num)
Run Code Online (Sandbox Code Playgroud)
当我尝试运行此代码段时,它一直有效,直到我的"num = 5",然后我得到了这个错误:
'int' object has no attribute 'status_code'
Exception Location: C:\Python27\lib\site-packages\django\middleware\common.py in process_response, line 94
Run Code Online (Sandbox Code Playgroud)
和Traceback说:
C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
response = middleware_method(request, response) ...
? Local vars
C:\Python27\lib\site-packages\django\middleware\common.py in process_response
if response.status_code == 404: ...
? Local vars
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?请你帮助我好吗 ?
我想实现一个处理屏幕开/关的服务.为此,我创建BootReceiver
了在启动时调用并在我启动服务的过程中调用.但是,我ScreenBroadcastReceiver
从未被称为,为什么?我是否在错误的地方注册了,我在服务的创建上做了...请帮助我在哪里注册?
因此,应用程序应该在后台运行(没有用户交互),它将记录屏幕的开启和关闭活动.我已经启动了这些代码,但似乎我需要注册我的ScreenBroadcastReceiver广播接收器.如果我在服务的创建上做.我该改变吗?我的最终目标是在后台运行此应用程序,以便应用程序无需用户交互即可运行
这是在一开始就调用的BootReceiver:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("TAG", "BootReceiver");
Intent service = new Intent(context, ScreenListenerService.class);
context.startService(service);
}
}
Run Code Online (Sandbox Code Playgroud)
这是服务:
public class ScreenListenerService extends Service {
public void onCreate(){
super.onCreate();
Log.w("TAG", "ScreenListenerService---OnCreate ");
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver screenOnReceiver = new ScreenBroadcastReceiver();
registerReceiver(screenOnReceiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w("TAG", "ScreenListenerService---onStart ");
return START_STICKY;
}
@Override
public IBinder onBind(Intent …
Run Code Online (Sandbox Code Playgroud) 我有一个非常基本的python脚本,每分钟运行一次crontab.
剧本
filed = open('test.txt','a')
Run Code Online (Sandbox Code Playgroud)
crontab的
* * * * * /to path the file/job.py
Run Code Online (Sandbox Code Playgroud)
它应该工作,但我无法看到结果.那么,可能是什么问题呢?
我有下一个型号:
class People(models.Model):
name = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
class Salary(models.Model):
id_of_people=models.ForeignKey(People)
salary = models.IntegerField(required=False)
Run Code Online (Sandbox Code Playgroud)
在views.py中
- 当我尝试这个时:
for each in People.objects.all()[:3]:
Salary().id_of_people_id=each.id
Salary().save()
Run Code Online (Sandbox Code Playgroud)
它只保存3 Th id,而不是1到3.因此,它只节省一次.但是,我想循环遍历所有3条记录,为什么会这样?如何将所有People表ID添加到Salary表?
假设我有一个字符串,/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherthing
我只想提取'0-1-2-3-4-5'部分.我试过这个:
str='/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherhing'
print str[str.find("-")-1:str.find("-")]
Run Code Online (Sandbox Code Playgroud)
但是,结果只有0.如何提取'0-1-2-3-4-5'部分?
c=['a,b,c','a,b,c','a,b']
for item in c:
a=set(item.replace("'",""))
b=list(a)
print b
Run Code Online (Sandbox Code Playgroud)
它返回:
['a','b',',']
Run Code Online (Sandbox Code Playgroud)
我想要的是:
['a','b','c']
Run Code Online (Sandbox Code Playgroud)
如何将这些字符串转换为字符集?
我想实现一个处理屏幕开/关的服务.为此,我创建BootReceiver
了在启动时调用并在我启动服务的过程中调用.
但是,OnCreate
从来没有被称为,为什么?
当我打印程序onCreate
的日志时,即使看到日志,我也看不到onStartCommand
日志.这怎么可能?请帮我理解这个.
这是BootReceiver
在一开始就调用的:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("TAG", "BootReceiver");
Intent service = new Intent(context, ScreenListenerService.class);
context.startService(service);
}
}
Run Code Online (Sandbox Code Playgroud)
这是服务:
public class ScreenListenerService extends Service {
public void OnCreate(){
super.onCreate();
Log.w("TAG", "ScreenListenerService---OnCreate ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w("TAG", "ScreenListenerService---onStart ");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return …
Run Code Online (Sandbox Code Playgroud) python ×8
django ×5
android ×3
cron ×1
django-views ×1
linux ×1
list ×1
matplotlib ×1
ubuntu ×1