小编raz*_*anc的帖子

使用ConfigParser将注释写入文件

如何在段内的给定文件中写注释?

如果我有:

import ConfigParser
with open('./config.ini', 'w') as f:
    conf = ConfigParser.ConfigParser()
    conf.set('DEFAULT', 'test', 1)
    conf.write(f)
Run Code Online (Sandbox Code Playgroud)

我会得到这个文件:

[DEFAULT]
test = 1
Run Code Online (Sandbox Code Playgroud)

但是如何在[DEFAULT]部分内部获得包含注释的文件,例如:

[DEFAULT]
; test comment
test = 1
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过以下方式将代码写入文件:

import ConfigParser
with open('./config.ini', 'w') as f:
    conf = ConfigParser.ConfigParser()
    conf.set('DEFAULT', 'test', 1)
    conf.write(f)
    f.write('; test comment') # but this gets printed after the section key-value pairs
Run Code Online (Sandbox Code Playgroud)

这是ConfigParser的可能吗?而且我不想尝试另一个模块,因为我需要尽可能地将我的程序保持为"库存".

python configparser

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

matplotlib标准色彩映射用法

我正在使用matplotlib 1.3.0,我有以下内容:

import matplotlib.pyplot as plt
cmap = plt.cm.jet
plt.contourf([[.12, .2], [.8, 2]], levels=[0, .1, .3, .5, 1, 3], cmap=cmap, vmin=0, vmax=3)
plt.colorbar()
Run Code Online (Sandbox Code Playgroud)

产生:

在此输入图像描述

我不明白的是所有其他颜色在哪里?据我所知,通过指定vmin=0,vmax=3颜色条应该使用cmap此图像中的全部范围:

在此输入图像描述

其没有给出所产生的vmin,vmaxlevels参数.那么......我在这里错过了什么?

编辑1

回应tom10&tcaswell.我原以为它会像你说的那样,但是......不幸的是,它不是.看看这个:

plt.contourf([[.12, .2], [.8, 3.2]], levels=[0, .1, .3, .5, 1, 3], cmap=cmap, vmin=0, vmax=3)
plt.colorbar()
Run Code Online (Sandbox Code Playgroud)

有:

在此输入图像描述

也许是为了澄清这一点:说我有数据,它的重要特征是大约0.1,但有一些约3让我们说.所以我给它一个levels=[0, 0.005, 0.075, 0.1, 0.125, 0.15, 0.2, 1, 2.5, 2.75, 3, 3.25]vmin=0, vmax=3.25.现在我希望看到全范围的颜色,但所有重要的数据点0.005到0.125最终都在蓝色区域(通过使用标准plt.cm.jet颜色图).我想说的是......如果我给出levels=[0, 1, 2, 3], vmin=0, vmax=3 …

matplotlib color-mapping colorbar

8
推荐指数
1
解决办法
2万
查看次数