ValueError: 'box' is not a valid value for adjustable; supported values are 'box-forced', 'datalim'
时间: 2023-07-30 16:09:42 浏览: 334
在 Matplotlib 绘制图形时出现 `ValueError: 'box' is not a valid value for adjustable; supported values are 'box-forced', 'datalim'` 错误,是因为 `ax.set_aspect()` 函数的 `adjustable` 参数传入了错误的值。
`ax.set_aspect()` 函数用于设置坐标轴的宽高比。其中 `adjustable` 参数用于控制坐标轴的可调性,支持的值有 `'box-forced'` 和 `'datalim'`。当该参数的值为 `'box'` 时,就会出现上述错误。
解决方法是将 `adjustable` 参数的值改为 `'box-forced'` 或 `'datalim'`,例如:
```python
ax.set_aspect('box-forced')
```
或者
```python
ax.set_aspect('datalim')
```
这样就可以避免出现该错误。
相关问题
ValueError: None is not a valid value for reduction
这是一个技术问题,我可以回答。这个错误通常出现在使用 PyTorch 进行深度学习训练时,损失函数的 reduction 参数设置为 None,但是在计算损失时没有指定 reduction 方法,导致出现错误。解决方法是将 reduction 参数设置为 'mean' 或 'sum',或者在计算损失时指定 reduction 方法。
ValueError: '...' is not a valid value for ls; supported values are '-', '--', '-.', ':', 'None', ' ', '', 'solid', 'dashed', 'dashdot', 'dotted'
<<
这个 `ValueError` 错误提示你在使用 Matplotlib 的线条样式(line style)参数时,传入了一个无效值。Matplotlib 支持的线条样式包括以下几种:
- `'-'` 或 `'solid'`: 实线
- `'--'` 或 `'dashed'`: 虚线
- `'-.'` 或 `'dashdot'`: 点划线
- `':'` 或 `'dotted'`: 点线
- `'None'`, `' '`, or `''`: 没有线条
如果你传递了其他字符串或不支持的值,则会抛出上述错误。
### 解决方法
确保你为 `ls` 参数提供了有效的值。例如,如果你想绘制一条点划线,应将 `ls='-.'` 或 `ls='dashdot'` 作为输入。
以下是正确使用的代码示例:
```python
import matplotlib.pyplot as plt
# 正确设置线条样式的例子
x = [0, 1, 2, 3]
y = [0, 1, 4, 9]
plt.plot(x, y, ls='-', label="Solid line") # 实线
plt.plot(x, y, ls='--', label="Dashed line") # 虚线
plt.plot(x, y, ls='-.', label="Dash-dot line") # 点划线
plt.plot(x, y, ls=':', label="Dotted line") # 点线
plt.legend()
plt.show()
```
如果出现该错误是因为拼写问题或其他非法字符导致的,请检查并修正这些值。
---
### 给出解释
此错误通常发生在调用 Matplotlib 中与绘图相关的函数(如 `plot()`、`ax.plot()` 等),并且尝试自定义线条属性的时候。比如当你希望指定某种特定类型的线条但没有按照官方文档中列出的有效选项操作就会引发这个问题。
#### 示例中的潜在错误场景:
假如你在编写类似下面这样的代码时候出现了笔误:
```python
plt.plot([1, 2, 3], [4, 5, 6], linestyle="---")
```
这里的 `"---"` 并不是一个合法的选择, 所以 Python 就报出了 `ValueError`.
正确的应该是选择像 `'--'`(表示虚线) 这样的预设样式之一。
---
阅读全文
相关推荐

















