在此之后,我成功地按照我想要的方式旋转标签。
axes = pd.plotting.scatter_matrix(df_plot, alpha=0.1, figsize=[10, 10])
n = len(df_plot.columns) - 1
for x in range(n):
for y in range(n):
# to get the axis of subplots
ax = axes[x, y]
# to make x axis name vertical
ax.xaxis.label.set_rotation(90)
# to make y axis name horizontal
ax.yaxis.label.set_rotation(0)
# to make sure y axis names are outside the plot area
ax.yaxis.labelpad = 25
Run Code Online (Sandbox Code Playgroud)
现在的问题是(1)我需要手动调整标签板(这不是一个选项,因为我有许多从循环创建的此类图)和(2)较长的标签被图形边缘截断(这可以通过 plt.tight_layout() 部分解决,但这也增加了我不想要的散点图之间的空间)。我怎样才能解决这个问题?我想一定有简单的“自动调整”?
我正在使用置换测试(抽取随机子样本)来测试2个实验之间的差异。每个实验进行100次(每个实验= 100个副本)。每个副本随时间包含801个测量点。现在,我想执行一种排列(或引导捆绑)操作,以测试每个实验需要多少副本(以及多少个(时间)测量点)才能获得一定的可靠性。
为此,我编写了一个代码,从中提取了最小的工作示例(其中很多东西都进行了硬编码)(请参见下文)。输入数据生成为随机数。此处为100个副本和801个时间点的np.random.rand(100,801)。
该代码原则上可以工作,但是如果选择随机子样本5000次,则有时所产生的曲线有时不会平滑地下降。这是以下代码的输出:
可以看出,在x轴的2处有一个不应该出现的峰值。如果我将随机种子从52389更改为324235,它将消失并且曲线平滑。随机数的选择方式似乎有问题吗?
为什么会这样呢?我在Matlab中有语义相似的代码,并且在已经有1000个排列(此处为5000个)的情况下,曲线完全平滑。
我有编码错误还是numpy随机数生成器不好?
有人在这里看到问题吗?
import matplotlib.pyplot as plt
import numpy as np
from multiprocessing import current_process, cpu_count, Process, Queue
import matplotlib.pylab as pl
def groupDiffsInParallel (queue, d1, d2, nrOfReplicas, nrOfPermuts, timesOfInterestFramesIter):
allResults = np.zeros([nrOfReplicas, nrOfPermuts]) # e.g. 100 x 3000
for repsPerGroupIdx in range(1, nrOfReplicas + 1):
for permutIdx in range(nrOfPermuts):
d1TimeCut = d1[:, 0:int(timesOfInterestFramesIter)]
d1Idxs = np.random.randint(0, nrOfReplicas, size=repsPerGroupIdx)
d1Sel = d1TimeCut[d1Idxs, :]
d1Mean = np.mean(d1Sel.flatten())
d2TimeCut = d2[:, 0:int(timesOfInterestFramesIter)]
d2Idxs = np.random.randint(0, nrOfReplicas, size=repsPerGroupIdx) …Run Code Online (Sandbox Code Playgroud) 这应该有效,但没有?
plt.figure()
plt.plot(x)
plt.xticks(range(4), [2, 64, 77, 89]) # works
f, ax = plt.subplots(nrows=2, ncols=2)
ax[0, 0].plot(x)
ax[0, 0].set_xticks(range(4), [2, 64, 77, 89]) # does not work
Run Code Online (Sandbox Code Playgroud) 我想向现有数据多维数据集添加另一个“切片”数据:
import numpy as np
a = np.array([[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]])
print(a.shape) # (2, 3, 4)
b = np.array([[7, 7, 7], [8, 8, 8]])
print(b.shape) # (2, 3)
c = np.concatenate([a, b], axis=2) # ValueError: all the input arrays must have same number of dimensions
print(c.shape) # wanted result: (2, 3, 5)
Run Code Online (Sandbox Code Playgroud)
因此,我基本上想通过将最后一个尺寸扩展到2x3x5,将2x3数组添加到2x3x4数组中。值错误对我来说不太有意义,因为数组的形状不能相同?我在这里做错了什么?
我遍历从JSON文件,它工作正常,但只要我删除一些条目中创建一个字典else子句结果的变化(通常它打印35个nuts_ids但随着remove中else只有32个被打印出来。这样看来,移除会影响迭代,但是为什么呢?密钥应该是安全的?如何在不丢失数据的情况下适当地做到这一点?
import json
with open("test.json") as json_file:
json_data = json.load(json_file)
for g in json_data["features"]:
poly = g["geometry"]
cntr_code = g["properties"]["CNTR_CODE"]
nuts_id = g["properties"]["NUTS_ID"]
name = g["properties"]["NUTS_NAME"]
if cntr_code == "AT":
print(nuts_id)
# do plotting etc
else: # delete it if it is not part a specific country
json_data["features"].remove(g) # line in question
# do something else with the json_data
Run Code Online (Sandbox Code Playgroud) 我有一个 json 文件,我尝试像这样加载:
import json
data = json.loads('test.json')
Run Code Online (Sandbox Code Playgroud)
但它崩溃了:
Traceback (most recent call last):
File "XXXXXXX.py", line 61, in <module>
data = json.loads('test.json')
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Run Code Online (Sandbox Code Playgroud)
当用 vi 打开时,该行看起来没问题:
{"922": {"ticket": {"problem": [{"id": ...
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
python ×6
json ×2
numpy ×2
dictionary ×1
matplotlib ×1
pandas ×1
permutation ×1
plot ×1
random ×1