小编fue*_*ika的帖子

在matplotlib colorbar中隐藏每个第n个刻度标签最干净的方法?

我的水平颜色条上的标签太靠近了,我不想进一步减小文字大小:

cbar = plt.colorbar(shrink=0.8, orientation='horizontal', extend='both', pad=0.02)
cbar.ax.tick_params(labelsize=8)
Run Code Online (Sandbox Code Playgroud)

带有坏标签的水平颜色条

我想保留所有刻度,但删除所有其他标签.

我发现的大多数示例都将用户指定的字符串列表传递给cbar.set_ticklabels().我正在寻找一个通用的解决方案.

我玩弄各种变化

cbar.set_ticklabels(cbar.get_ticklabels()[::2])
Run Code Online (Sandbox Code Playgroud)

cbar.ax.xaxis.set_major_locator(matplotlib.ticker.MaxNLocator(nbins=4))
Run Code Online (Sandbox Code Playgroud)

但我还没有找到神奇的组合.

我知道必须有一个干净的方法来使用定位器对象.

python matplotlib

35
推荐指数
4
解决办法
3万
查看次数

pip无法安装任何东西

> pip install yolk
Downloading/unpacking yolk
  Cannot fetch index base URL https://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement yolk
No distributions at all found for yolk
Storing complete log in /Users/harith/.pip/pip.log
Run Code Online (Sandbox Code Playgroud)

当我看到我看到的文件时

> cat /Users/harith/.pip/pip.log
------------------------------------------------------------
/Users/harith/.shared/virtualenvs/pennytracker/bin/pip run on Mon Jul  1 20:26:02 2013
Downloading/unpacking yolk

  Getting page https://pypi.python.org/simple/yolk/
  Could not fetch URL https://pypi.python.org/simple/yolk/: HTTP Error 503: Service Unavailable
  Will skip URL https://pypi.python.org/simple/yolk/ when looking for download links for yolk
  Getting page https://pypi.python.org/simple/
  Could not fetch URL …
Run Code Online (Sandbox Code Playgroud)

python pip

32
推荐指数
4
解决办法
12万
查看次数

在Ubuntu中永久更改主机名

我想创建一个shell脚本,可以永久地更改我的Ubuntu的主机名.每当我使用该hostname New_hostname命令时,它会在我重新启动机器后返回到原始主机名.

我发现我可以永久改变它的唯一方法是修改文件/etc/hostname并保存.有没有办法只使用shell脚本才能做到这一点?我也有密码.

linux shell scripting ubuntu

12
推荐指数
4
解决办法
1万
查看次数

如何编写一个切片的函数?

我想在Python中编写一个函数,它将一个切片作为参数.理想情况下,用户可以按如下方式调用该函数:

foo(a:b:c)
Run Code Online (Sandbox Code Playgroud)

不幸的是,这种语法不Python允许-使用a:b:c只允许范围内[],没有().

因此,我认为我的功能有三种可能性:

  1. 要求用户使用切片"构造函数"(其s_行为类似于numpy提供的版本):

    foo(slice(a, b, c))
    foo(s_[a:b:c])
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将我的函数的逻辑放入一个__getitem__方法:

    foo[a:b:c]
    
    Run Code Online (Sandbox Code Playgroud)
  3. 放弃尝试切片并单独开始,停止和步骤:

    foo(a, b, c)
    
    Run Code Online (Sandbox Code Playgroud)

有没有办法让原始语法工作?如果不是,首选哪种解决方法语法?或者还有另一种更好的选择吗?

python numpy slice

11
推荐指数
1
解决办法
416
查看次数

将微秒转换为人类可读时间

我需要将两个动作之间的时差(差异)转换为人类可读时间.

我怎么能用python做到这一点?我试过类似的东西

    diff = 49503757
    datetime.time(0,0,0,diff)
Run Code Online (Sandbox Code Playgroud)

但是diff值太长,datetime期望在0到999999之间的微秒值,在这个例子中我的差异是49503757.

python time

5
推荐指数
1
解决办法
5174
查看次数

从.tar-archive有效地提取单个文件

我有一个.tgz大小为2GB 的文件.

我想.txt从.tgz文件中只提取一个大小为2KB的文件.

我有以下代码:

import tarfile
from contextlib import closing

with closing(tarfile.open("myfile.tgz")) as tar:
    subdir_and_files = [
        tarinfo for tarinfo in tar.getmembers()
        if tarinfo.name.startswith("myfile/first/second/text.txt")
        ]
    print subdir_and_files
    tar.extractall(members=subdir_and_files)
Run Code Online (Sandbox Code Playgroud)

问题是我得到提取的文件至少需要一分钟.似乎extractall提取所有文件,但只保存我问的那个.

有没有更有效的方法来实现它?

python

5
推荐指数
1
解决办法
1441
查看次数

生成随机浮点数

现在我正在努力在你的屏幕上制作Python"类型"(我感到无聊和实验),而且我自己得到了这么多,但我找不到任何方法在我的脚本中使用小数,如Python只接受randint()的整数.

#include <stdio.h>;
import sys;
import time;
import os;
import random;
os.system("clear");

script = "I just really need to type something right now. I'm not sure why, but I really feel like typing on the Chromebook right now. It seems kinda weird, but I like doing this. It helps me practice what I'm doing. I've noticed that I'm a lot worse at typing lately, and I don't know why. Maybe it's because I've been working with a chunky keyboard, and …
Run Code Online (Sandbox Code Playgroud)

python

-1
推荐指数
1
解决办法
2701
查看次数

标签 统计

python ×6

linux ×1

matplotlib ×1

numpy ×1

pip ×1

scripting ×1

shell ×1

slice ×1

time ×1

ubuntu ×1