plt 画图原点在左上角
时间: 2023-11-02 16:06:23 浏览: 106
可以通过设置坐标轴的方向来改变 plt 画图原点的位置。可以使用 `plt.gca().invert_yaxis()` 来使原点位于左下角。具体代码如下:
```python
import matplotlib.pyplot as plt
# 生成一些样本数据
x = [1, 2, 3]
y = [4, 5, 6]
# 绘制散点图,并设置 x 轴和 y 轴的范围
plt.scatter(x, y)
plt.xlim(0, 4)
plt.ylim(0, 7)
# 将 y 轴反向
plt.gca().invert_yaxis()
# 显示图像
plt.show()
```
这样,图像的原点就会位于左下角了。
相关问题
matplotlib 左上角原点
要将 Matplotlib 图表的原点移动到左上角,可以通过以下步骤实现:
1. 导入所需的模块和函数:
```python
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
```
2. 创建一个图表和一个轴对象:
```python
figure, ax = plt.subplots()
```
3. 设置 x 和 y 值的范围:
```python
ax.set_xlim(left=0, right=20)
ax.set_ylim(bottom=10, top=0)
```
4. 将 x 坐标标记移到上方:
```python
ax.xaxis.tick_top()
```
5. 创建需要绘制的线条数据:
```python
line1 = [(1, 1), (5, 5)]
line2 = [(11, 9), (8, 8)]
```
6. 将线条数据分别解压为 x 和 y 坐标的列表:
```python
(line1_xs, line1_ys) = zip(*line1)
(line2_xs, line2_ys) = zip(*line2)
```
7. 在轴对象上添加线条对象:
```python
ax.add_line(Line2D(line1_xs, line1_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line2_xs, line2_ys, linewidth=1, color='red'))
```
8. 显示图表:
```python
plt.plot()
plt.show()
```
通过以上步骤,你可以将 Matplotlib 图表的原点移动到左上角。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [matplotlib 坐标原点移到左上角](https://blog.csdn.net/qq_16912257/article/details/53887597)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [matplotlib 设置左上角为坐标轴原点](https://blog.csdn.net/waydong/article/details/78989552)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
python画图时,把plt.legend调整标签的位置在左上角
可以使用plt.legend()函数的loc参数来设置图例的位置。例如,要将图例放置在左上角,可以将loc设置为'upper left'或者2:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 5, 8, 3]
plt.plot(x, y, label='data')
plt.legend(loc='upper left')
plt.show()
```
除了'upper left'和2,还可以使用其他字符串或数字来设置图例的位置。可以参考matplotlib文档中关于legend()函数的说明:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html