python - 如何在 matplotlib 网格的特定单元格中着色?
问题描述
我正在尝试绘制一个网格,其中每个框中的箭头居中,并在特定单元格中着色。这就是我到目前为止所拥有的。可悲的是,箭头没有居中,但我尽力了。
arrow_direction_per_cell = {(0, 0): 'RIGHT',(0, 1): 'DOWN',(0, 2): 'UP',(0, 3): 'LEFT',(0, 4): 'UP',
(1, 0): 'RIGHT',(1, 1): 'RIGHT',(1, 2): 'DOWN',(1, 3): 'LEFT', (1, 4): 'LEFT',(2, 0): 'RIGHT',(2, 1): 'RIGHT',
(2, 2): 'DOWN',(2, 3): 'LEFT',(2, 4): 'UP',(3, 0): 'LEFT',(3, 1): 'RIGHT',(3, 2): 'UP',(3, 3): 'LEFT',(3, 4): 'UP',
(4, 0): 'LEFT',(4, 1): 'DOWN', (4, 2): 'UP',(4, 3): 'UP', (4, 4): 'UP'}
direction_map = {'UP':(0,1),'DOWN':(0,-1),'RIGHT':(1,0),'LEFT':(-1,0)}
locations = arrow_direction_per_cell.keys()
fig, ax = plt.subplots()
plt.xlim(0, 5)
plt.ylim(5, 0)
plt.grid(True)
plt.xticks(np.arange(0, 6, 1.0))
plt.yticks(np.arange(0, 6, 1.0))
for location in locations:
arrow_direction = arrow_direction_per_cell[location]
x_pos,y_pos = (location[0]+.5),(location[1]+.5)
x_direct,y_direct = direction_map[arrow_direction]
ax.quiver(x_pos, y_pos, x_direct, y_direct,scale=30)
cells_to_be_orange = [(0,2),(2,4),(3,4)]
cells_to_be_blue = [(3,2)]
ax.quiver(x_pos, y_pos, x_direct, y_direct)
plt.show()
如何在“cells_to_be_orange”橙色和“cells_to_be_blue”蓝色的位置上着色,以及如何使箭头看起来更居中?
非常感谢,祝您有美好的一天。
解决方案
您只需添加一个函数cell_color
并在ax.quiver()
.
该函数arrow_position
尝试使箭头居中,但您应该检查更好的解决方案。
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
arrow_direction_per_cell = {
(0, 0): 'RIGHT', (0, 1): 'DOWN' , (0, 2): 'UP' , (0, 3): 'LEFT', (0, 4): 'UP',
(1, 0): 'RIGHT', (1, 1): 'RIGHT', (1, 2): 'DOWN', (1, 3): 'LEFT', (1, 4): 'LEFT',
(2, 0): 'RIGHT', (2, 1): 'RIGHT', (2, 2): 'DOWN', (2, 3): 'LEFT', (2, 4): 'UP',
(3, 0): 'LEFT', (3, 1): 'RIGHT', (3, 2): 'UP' , (3, 3): 'LEFT', (3, 4): 'UP',
(4, 0): 'LEFT', (4, 1): 'DOWN' , (4, 2): 'UP' , (4, 3): 'UP' , (4, 4): 'UP'
}
direction_map = {
'UP' : ( 0, 1),
'DOWN' : ( 0,-1),
'RIGHT': ( 1, 0),
'LEFT' : (-1, 0)
}
def arrow_color(location):
cells_to_be_blue = [(3,2)]
cells_to_be_orange = [(0,2),(2,4),(3,4)]
if location in cells_to_be_orange:
return "orange"
elif location in cells_to_be_blue:
return "blue"
return "black"
def arrow_position(location, arrow_direction):
if arrow_direction == 'UP':
correction = (.5, .6)
elif arrow_direction == 'DOWN':
correction = (.5, .4)
elif arrow_direction == 'RIGHT':
correction = (.4, .5)
elif arrow_direction == 'LEFT':
correction = (.6, .5)
return (location[0]+correction[0],
location[1]+correction[1])
fig, ax = plt.subplots()
plt.xlim(0, 5)
plt.ylim(5, 0)
plt.grid(True)
plt.xticks(np.arange(0, 6, 1.0))
plt.yticks(np.arange(0, 6, 1.0))
locations = arrow_direction_per_cell.keys()
for location in locations:
arrow_direction = arrow_direction_per_cell[location]
x_pos, y_pos = arrow_position(location, arrow_direction)
x_direct, y_direct = direction_map[arrow_direction]
ax.quiver(
x_pos, y_pos,
x_direct, y_direct,
color=arrow_color(location),
scale=30)
plt.show()
推荐阅读
- rust - 如何检查 &str 是否包含 Rust 中的枚举?
- react-native - 为什么在使用 React Native Navigation Drawer 时出现组件异常
- sql - SQLite 子查询和内连接
- flutter - 在 dart 中设置和访问类变量没有按预期工作
- flutter - 如何使用颤振字体真棒图标?
- azure - 我们如何控制身份验证方案中的“SignUpSignInPolicyId”
- ffmpeg - ffmpeg 创建 mpeg-dash 块文件太慢导致 404 错误
- javascript - 我可以让我的不和谐机器人基于网站发送消息吗?
- reactjs - react redux中的删除操作不起作用
- postgresql - Postgres SERIALIZABLE Snapshot Isolation 出现意外的枢轴错误