我正在以两种不同的格式绘制相同的数据:对数比例和线性比例.
基本上我想要完全相同的情节,但有不同的尺度,一个在另一个的顶部.
我现在拥有的是:
import matplotlib.pyplot as plt
# These are the plot 'settings'
plt.xlabel('Size')
plt.ylabel('Time(s)');
plt.title('Matrix multiplication')
plt.xticks(xl, rotation=30, size='small')
plt.grid(True)
# Settings are ignored when using two subplots
plt.subplot(211)
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')
plt.subplot(212)
plt.yscale('log')
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')
Run Code Online (Sandbox Code Playgroud)
plt.subplot之前的所有'设置' 都将被忽略.
我可以按照我想要的方式工作,但是我必须在每个子图声明后复制所有设置.
有没有办法一次配置两个子图?
我有一个函数(我无法修改)试图打开一个文件但在进程中抛出一个异常(blackbox在我的MWE中,一个从模块实际导入的函数).我正在尝试编写一些异常处理代码来删除blackbox试图写入的垃圾文件.但是,我不能,因为文件仍然打开,我不知道如何在不知道文件句柄(myfile)的情况下关闭它.
这就是我的MWE的样子:
import os
def blackbox(mypath): # a function that I can't modify
myfile = open(mypath, 'w')
myfile.write('some text')
myfile.flush() # to make sure 'some text' is actually written to disc file.
raise Exception('An error occured')
myfile.write('some stuff that will never be written')
myfile.close()
def del_file(mypath):
# *** put something here that will close the file ***
os.remove(mypath) # throws an error because file is still in use
return
mypath = 'g:/test.txt'
try:
blackbox(mypath) …Run Code Online (Sandbox Code Playgroud) 我正在绘制一个带有几行的Pandas DataFrame,每行都有一个特定的颜色(由rgb值指定).我正在寻找一种方法,通过将绘图线颜色直接分配给DataFrame列名而不是按顺序列出它们,使我的代码更具可读性.
我知道我可以这样做:
import pandas as pd
df = pd.DataFrame(columns=['red zero line', 'blue one line'], data=[[0, 1], [0, 1]])
df.plot(colors = ['#BB0000', '#0000BB']) # red amd blue
Run Code Online (Sandbox Code Playgroud)
但是有两行以上,我真的希望能够按列标题指定颜色,以使代码易于维护.比如这样:
df.plot(colors = {'red zero line': '#FF0000', 'blue one line': '#0000FF'})
Run Code Online (Sandbox Code Playgroud)
尽管如此,colors关键字实际上不能是字典.(从技术上讲,它是类型转换为列表,它会生成列标签列表.)
我理解pd.DataFrame.plot继承自matplotlib.pyplot.plot但我找不到colors关键字的文档.这两种方法的文档都没有列出这样的关键字.
我正在编写一个 pytest 文件来检查我的机器学习库是否使用 GPU。对于 Tensorflow,我可以使用 进行检查tf.config.list_physical_devices()。对于 XGBoost,到目前为止,我已经通过nvdidia-smi在运行软件时查看 GPU 利用率 ( ) 来检查它。但我如何通过简单的测试来检查这一点呢?类似于我对 Tensorflow 的测试就可以做到。
import pytest
import tensorflow as tf
import xgboost
# Marking all tests to be GPU dependent
pytestmark = pytest.mark.gpu
def test_tf_finds_gpu():
"""Check if Tensorflow finds the GPU."""
assert tf.config.list_physical_devices("GPU")
def test_xgb_finds_gpu():
"""Check if XGBoost finds the GPU."""
...
# What can I write here?
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Google Earth Engine (GEE) 代码编辑器中获取图像集合中的图像数量。图像集filteredCollection包含 GEE 上覆盖格林威治的所有 Landsat 8 图像(只是一个示例)。
图像数量打印为 113,但它似乎不是整数类型,我也不能将其强制为整数。这是它的样子:
var imageCollection = ee.ImageCollection("LANDSAT/LC8_SR");
var point = ee.Geometry.Point([0.0, 51.48]);
var filteredCollection = imageCollection.filterBounds(point);
var number_of_images = filteredCollection.size();
print(number_of_images); // prints 113
print(number_of_images > 1); // prints false
print(+number_of_images); // prints NaN
print(parseInt(number_of_images, 10)); // prints NaN
print(Number(number_of_images)); // prints NaN
print(typeof number_of_images); // prints object
print(number_of_images.constructor); // prints <Function>
print(number_of_images.constructor.name); // prints Ik
var number_of_images_2 = filteredCollection.length;
print(number_of_images_2); // prints undefined
Run Code Online (Sandbox Code Playgroud)
知道这里发生了什么以及如何将集合中的图像数量作为整数获取吗?
PS:Collection.size() 是GEE …