【问题标题】:Stream plot with varying colours in matplotlib giving 2 cmapsmatplotlib 中不同颜色的流图给出 2 个 cmap
【发布时间】:2020-10-07 16:06:09
【问题描述】:

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()

stream plot 的帮助文件有一个示例,该示例可以解决此问题,并且可以按预期工作。这是我用来绘制原始流图的。

  1. Stream plot
  2. Constrained Layout Guide

总结

所以总结一下我的问题。怎么去掉旁边的两张色图?

任何帮助将不胜感激。

【问题讨论】:

  • 删除# Plot 2下的fig.colorbar(strm.lines)
  • @BigBen 我现在才注意到,感觉是最大的斩击。谢谢哈哈哈

标签: python matplotlib colors data-visualization subplot


【解决方案1】:

您应该指定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()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 2014-04-03
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多