matplotlib中不同颜色的流图,给出2个CMAP

2024-05-16 02:17:00 发布

您现在位置:Python中文网/ 问答频道 /正文

A stream plot, or streamline plot, is used to display 2D vector fields。我正在用Python创建一个具有不同颜色的流图,但同时得到两个不同的cmap。所使用的代码几乎与帮助文件相同,但我在第三个绘图上获得了多个CMAP。如何删除第二个cmap

下面是我使用的代码,后面是输出

import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline
x,y = np.meshgrid(np.linspace(-5,5,20),np.linspace(-5,5,20))

xdot = y
ydot = -2*x - 3*y

# subplot2grid
fig = plt.figure(figsize=(18,10))
ax1 = plt.subplot2grid((2,2), (0, 0))
ax2 = plt.subplot2grid((2,2), (0, 1))
ax3 = plt.subplot2grid((2,2), (1, 0))
ax4 = plt.subplot2grid((2,2), (1, 1))

# Plot 1
Q = ax1.quiver(x, y, xdot, ydot, scale=500, angles='xy') # Quiver key
ax1.quiverkey(Q,-10,22.5,30,'5.1.8',coordinates='data',color='k')
ax1.set(xlabel='x', ylabel='y')
ax1.set_title('Quiver plot 6.1.1')

# Plot 2
strm  = ax2.streamplot(x, y, xdot, ydot, density=1, color='k', linewidth=2) # streamplot(X,Y,u,v)
fig.colorbar(strm.lines)
ax2.set(xlabel='x', ylabel='y')
ax2.set_title('Stream plot of 6.1.1')

# Plot 4
strm  = ax4.streamplot(x, y, xdot, ydot, density=1, color=xdot, linewidth=2, cmap='autumn') # streamplot(X,Y,u,v, density = 1)
fig.colorbar(strm.lines, ax=ax4)
ax4.set(xlabel='x', ylabel='y', title='Stream plot of 6.1.1 with varying color')

plt.show()

enter image description here

stream plot的帮助文件中有一个示例,用于实现这个问题,该问题按预期工作。这是我用来绘制原始流图的

  1. Stream plot
  2. Constrained Layout Guide

总结

总结一下我的问题。如何删除侧面的两个颜色贴图

任何帮助都将不胜感激


Tags: plotnpfigpltcolorcmapsetxdot
1条回答
网友
1楼 · 发布于 2024-05-16 02:17:00

您应该指定ax2.streamplotax

import numpy as np
import matplotlib.pyplot as plt

x,y = np.meshgrid(np.linspace(-5,5,20),np.linspace(-5,5,20))

xdot = y
ydot = -2*x - 3*y

# subplot2grid
fig = plt.figure(figsize=(18,10))
ax1 = plt.subplot2grid((2,2), (0, 0))
ax2 = plt.subplot2grid((2,2), (0, 1))
ax3 = plt.subplot2grid((2,2), (1, 0))
ax4 = plt.subplot2grid((2,2), (1, 1))

# Plot 1
Q = ax1.quiver(x, y, xdot, ydot, scale=500, angles='xy') # Quiver key
ax1.quiverkey(Q,-10,22.5,30,'5.1.8',coordinates='data',color='k')
ax1.set(xlabel='x', ylabel='y')
ax1.set_title('Quiver plot 6.1.1')

# Plot 2
strm  = ax2.streamplot(x, y, xdot, ydot, density=1, color='k', linewidth=2) # streamplot(X,Y,u,v)
fig.colorbar(strm.lines, ax = ax2) # < - TO BE DELETED
ax2.set(xlabel='x', ylabel='y')
ax2.set_title('Stream plot of 6.1.1')

# Plot 4
strm  = ax4.streamplot(x, y, xdot, ydot, density=1, color=xdot, linewidth=2, cmap='autumn') # streamplot(X,Y,u,v, density = 1)
fig.colorbar(strm.lines, ax=ax4)
ax4.set(xlabel='x', ylabel='y', title='Stream plot of 6.1.1 with varying color')

plt.show()

enter image description here

或者,您可以删除上述代码行以删除不需要的颜色栏:

enter image description here

相关问题 更多 >