在matplotlib中使用不同颜色的流图提供2个cmap

时间:2020-06-17 18:17:14

标签: python matplotlib colors data-visualization subplot

一个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

摘要

所以总结我的问题。如何去除侧面的两个颜色图?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您应指定ax中的ax2.streamplot

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