matplotlib:绘制二维数组
所以我一直转圈圈在这最后一天了,希望有人可以把我赶出痛苦。matplotlib:绘制二维数组
我有一个函数f依赖于x和y的值,并在绘图˚F对Y,给出了以下figure。
现在每行的是对于x [0,1]的值和感觉,必须有一种方法来颜色/轮廓绘图,使得其可以很容易地鉴定出的线对应于x的什么值。我尝试了大量搜索,但在这种情况下没有找到任何有用的帮助。
的代码重现,让我的身材是如下数据。任何帮助都会很棒,因为我觉得我错过了一些显而易见的东西。
import numpy as np
import matplotlib.pyplot as plt
y = np.linspace(0,100)
x = np.linspace(0,1,11)
f = np.zeros((len(y), len(x)))
for j in range(len(x)):
i=0
for r in y:
f[i,j] = (1000 + (-r * 2)) + (1000*(1-x[j]))
i += 1
plt.plot(f, y)
import numpy as np
import matplotlib.pyplot as plt
y = np.linspace(0, 100)
x = np.linspace(0, 1, 11)
f = np.zeros((len(y), len(x)))
for j in range(len(x)):
i = 0
for r in y:
f[i, j] = (1000 + (-r * 2)) + (1000 * (1 - x[j]))
i += 1
plt.plot(f, y)
labels = [f[xs, 0] for xs in x]
plt.legend(labels, loc='best')
plt.show()
刚刚修复标签
对不起,我可能应该在原文中提到我不想使用图例,因为我有另一个类似的功能,它们可以一起绘制,并且图例很快就会失控。谢谢你的回应。 – Matthew
我给我的评论几种可能性:
或者是策划在 3D,或 annotations,或 colormapping the lines, 或者你只有非常有限的x值使用不同的 linestyle 每个。
除此之外,您可以创建专门用于x的新轴。在适于从以下代码段我把X值在顶部水平轴:
import numpy as np
import matplotlib.pyplot as plt
y = np.linspace(0,100)
x = np.linspace(0,1,11)
f = np.zeros((len(y), len(x)))
for j in range(len(x)):
i=0
for r in y:
f[i,j] = (1000 + (-r * 2)) + (1000*(1-x[j]))
i += 1
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
ax2.set_xticks(x)
ax2.set_xticklabels(["%.3f" % xi for xi in x])
ax1.plot(f, y)
结果如下:
感谢链接的例子,它们非常有帮助! – Matthew
由于线对应于多个或不太连续的x值,我会根据色彩图对线进行着色。 然后使用colorbar
来显示x
到颜色的映射。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cmx
import matplotlib.colors as colors
y = np.linspace(0,100)
x = np.linspace(0,1,11)
f = np.zeros((len(y), len(x)))
for j in range(len(x)):
i=0
for r in y:
f[i,j] = (1000 + (-r * 2)) + (1000*(1-x[j]))
i += 1
cn = colors.Normalize(vmin=0, vmax=1)
scalar_map = cmx.ScalarMappable(norm=cn, cmap='jet')
# see plt.colormaps() for many more colormaps
for f_, x_ in zip(f.T, x):
c = scalar_map.to_rgba(x_)
plt.plot(f_, y, color=c)
scalar_map.set_array([]) # dunno why we need this. plt.colorbar fails otherwise.
plt.colorbar(scalar_map, label='x')
这正是我想要的,谢谢你! – Matthew
怎么样一个[传奇](http://matplotlib.org/users/legend_guide.html)? – MKesper
http://stackoverflow.com/questions/16992038/inline-labels-in-matplotlib和http://matplotlib.org/users/legend_guide.html – Dadep
或者标绘在[3D](http://matplotlib.org/ mpl_toolkits/mplot3d/tutorial.html),或[注释](http://matplotlib.org/users/annotations_intro.html),或[colormapping线(http://stackoverflow.com/questions/8945699/gnuplot- linecolor-variable-in-matplotlib/18516488#18516488),或者如果只有非常有限的x个值,请使用不同的[linestyle](http://matplotlib.org/api/lines_api.html#matplotlib.lines。 Line2D.set_linestyle)。 – armatita