我正在尝试使用twiny将第二个x轴添加到绘图的顶部.
如果我创建一个没有颜色条的简单散点图,则顶部x轴与底部x轴正确对齐(MWE在下面):
如果我添加了一个颜色条,则顶部的x轴会移位:
我怎样才能解决这个问题?
MWE
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.gridspec as gridspec
X = np.array([0., 0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4.])
X2 = np.array([122, 85, 63, 50, 23, 12, 7, 5, 2])
Y = np.cos(X*20)
Z = np.sin(X*20)
fig = plt.figure()
gs = gridspec.GridSpec(1, 2)
ax1 = plt.subplot(gs[1])
ax2 = ax1.twiny()
ax1.set_xlim(-0.2, max(X)+0.2)
plt.tick_params(axis='both', which='major', labelsize=10)
ax1.minorticks_on()
ax1.grid(b=True, which='major', color='gray', linestyle='--', lw=0.3)
SC = ax1.scatter(X, Y, c=Z) …Run Code Online (Sandbox Code Playgroud) 我有一套(x, y, z)点,我需要找到最适合他们的飞机.平面由其系数定义为:
a*x + b*y + c*z + d = 0
Run Code Online (Sandbox Code Playgroud)
或等效地:
A*X +B*y + C = z
Run Code Online (Sandbox Code Playgroud)
第二个等式只是对第一个等式的重写.
我正在使用这个要点中开发的方法,这是从本答案中给出的Matlab代码到Python的翻译.该方法找到系数以定义最适合该组点的平面方程.
问题在于我能够提出一组系数,以便更好地适应这组点.
为了定义"更好",我按照这里给出的数学计算每个点到给定平面的绝对距离之和.较小的值意味着"更好"的拟合,因为这些点然后平均更接近平面.
MWE在下面.如可以看到的,手工采摘系数导致绝对距离值(较小总和~155.89),除了使用由(上述方法找到"最好"的系数~158.78).
我在这里错过了什么?
MWE
import numpy as np
import scipy.linalg
def sum_dist_2_plane(x, y, z, a, b, c, d):
"""
Sum of the absolute values of the distances to a plane, given by the
a,b,c,d coefficients, for the set of points defined by x,y,z.
""" …Run Code Online (Sandbox Code Playgroud) 我需要使用set_title()为绘图绘制标题,其中其背景以某种程度的透明度显示。
我已经尝试了三种方法(一种取自这个答案),但似乎都没有奏效。它们要么使文本的字体或边缘透明,但不是背景本身。
MWE:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1)
ax.minorticks_on()
ax.grid(b=True, which='minor', color='k', linestyle='--', lw=0.5,
zorder=1)
# Method 1
ax.set_title("Title", x=0.5, y=0.92, fontsize=13, alpha=0.2,
bbox=dict(facecolor='none'))
# Method 2
# ax.set_title("Title", x=0.5, y=0.92, fontsize=13,
# bbox=dict(facecolor='none', alpha=0.2))
# Method 3
# t = ax.set_title("Title", x=0.5, y=0.92, fontsize=13)
# t.set_bbox(dict(facecolor='none', alpha=0.2, edgecolor='k'))
plt.savefig('test.png')
Run Code Online (Sandbox Code Playgroud)
输出:
我正试图从git add命令中获取消息,以便稍后打印到日志文件.
import subprocess
import os
filename = 'test.txt'
# Add changes
add_cmd = """git add "%s" """ % filename
os.system(add_cmd)
a = subprocess.check_output(add_cmd, shell=True, stderr=subprocess.STDOUT)
Run Code Online (Sandbox Code Playgroud)
该os.system()电话显示屏幕:
fatal: Not a git repository (or any of the parent directories): .git
Run Code Online (Sandbox Code Playgroud)
这是正确的,因为这个文件夹不是git回购.
但是subprocess.check_output()呼叫失败了:
File "test.py", line 11, in <module>
a = subprocess.check_output(add_cmd, shell=True, stderr=subprocess.STDOUT)
File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'git add "test.txt" ' returned non-zero exit status …Run Code Online (Sandbox Code Playgroud) 我正在使用Sublime Text 3加上Git包来管理我的回购.我将.gitconfig文件设置为使用Meld工具(Meld v3.14.2):
[diff]
external = meld
tool = meld
Run Code Online (Sandbox Code Playgroud)
我可以从命令提示符启动$ git difftool,没有任何问题.
我曾经能够通过Git Diff Tool Current File和Git Diff Tool All命令对git包做同样的事情.重新安装Sublime Text 3后,当我尝试执行这两个操作中的任何一个时,Sublime底部的窗口出现,询问:
Viewing (1/1): 'test.py'
Launch 'meld' [Y/n]:
Run Code Online (Sandbox Code Playgroud)
我不能在这里输入任何文字,而且我之前不需要.如果我尝试Git Diff Current file它会打开一个新选项卡:
Usage:
meld Iniciar con una ventana vacía
meld <archivo|carpeta> Iniciar una comparación de control de versiones
meld <archivo> <archivo> [<archivo>] Iniciar una comparación de archivo de 2 ó 3 vías
meld <carpeta> …Run Code Online (Sandbox Code Playgroud) 我在我的系统中安装了pyenv + virtualenv和Anaconda,以管理虚拟环境.
我的.zshrc文件如下所示:
# Load pyenv automatically:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
# added by Anaconda3 4.2.0 installer
export PATH="$HOME/anaconda3/bin:$PATH"
Run Code Online (Sandbox Code Playgroud)
在给定的文件夹中,我设置了一个本地pyenv虚拟环境
$ pyenv local test-env
Run Code Online (Sandbox Code Playgroud)
列出该pyenv文件夹中的virtual-env会导致:
$ pyenv versions
system
2.7.12
2.7.12/envs/test-env
* test-env (set by /home/<path>/.python-version)
Run Code Online (Sandbox Code Playgroud)
但是当我加载python而不是用Python v2.7.12打开会话时(正如人们所期望的那样,因为pyenv加载了虚拟环境),我得到:
$ python
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or …Run Code Online (Sandbox Code Playgroud) 显示此错误的最小工作示例:
from os import listdir, getcwd
from os.path import isfile, join, realpath, dirname
import csv
def gd(mypath, myfile):
# Obtain the number of columns in the data file
with open(myfile) as f:
reader = csv.reader(f, delimiter=' ', skipinitialspace=True)
for i in range(20):
row_20 = next(reader)
# Save number of clumns in 'num_cols'.
num_cols = len(row_20)
return num_cols
mypath = realpath(join(getcwd(), dirname(__file__)))
# Iterate through all files. Stores name of file in 'myfile'.
for myfile in listdir(mypath):
if isfile(join(mypath,myfile)) and (myfile.endswith('.dat')): …Run Code Online (Sandbox Code Playgroud) 我的代码的最小工作示例:
# Create output data file
out_data_file = open('output_file','w')
out_data_file.write("# Header \n")
out_data_file.close()
list1 = [1,3,4,5,12,6,2,35,74,6,2]
# Open file to append
with open('output_file', "a") as f:
f.write('Text'+' '+str(['%d' % item for item in list1]))
Run Code Online (Sandbox Code Playgroud)
这给了我一个output_file看起来像:
# Header
Text ['1', '3', '4', '5', '12', '6', '2', '35', '74', '6', '2']
Run Code Online (Sandbox Code Playgroud)
我希望输出看起来像这样:
# Header
Text 1 3 4 5 12 6 2 35 74 6 2
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
假设我有一个由多个列表组成的列表,每个列表包含两个项目:
a = [[14, 0.5], [12, 0.8], [22, 0.6], [15, 0.2], [17, 0.5], [18, 0.4]]
Run Code Online (Sandbox Code Playgroud)
我需要先根据每个列表中的第二项从最大到最小重新排序此列表,然后根据从最小到最大的第一项.
我知道如何使用第二项和第一项第二项根据两项重新排序列表:
b = sorted(a, key=lambda item:(item[1], item[0]))
b = [[15, 0.2], [18, 0.4], [14, 0.5], [17, 0.5], [22, 0.6], [12, 0.8]]
Run Code Online (Sandbox Code Playgroud)
但这种返回从排序的列表最小到最大的这两个项目,这不是我所需要的.
这b应该是这样的:
b = [[12, 0.8], [22, 0.6], [14, 0.5], [17, 0.5], [18, 0.4], [15, 0.2]]
Run Code Online (Sandbox Code Playgroud) 我有一个列表,其中包含几个非常大的值,用于区分这些索引,它看起来像这样:
a = [1.3, 2.1, 9999., 5., 3.7 ,6.6, 9999., 7.4, 9999., 3.5, 7, 1.2, 9999.]
Run Code Online (Sandbox Code Playgroud)
我需要找到该列表其第二大值不等于9999.(在它上面会出现这种情况7.4),能够以最有效的方式(我的列表可以得到相当大的)
在这个问题中从包含100,000个整数的列表中检索两个最高项目,heapq.nlargest提到了该函数,但由于我有多个值9999.,因此无效.
python ×9
git ×2
list ×2
matplotlib ×2
anaconda ×1
diff ×1
file-io ×1
git-difftool ×1
plot ×1
pyenv ×1
scipy ×1
stderr ×1
stdout ×1
sublimetext3 ×1
subprocess ×1
transparency ×1