如何在一个图中得到不同地块的不同颜色的线? [英] How to get different colored lines for different plots in a single figure?

查看:394
本文介绍了如何在一个图中得到不同地块的不同颜色的线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 matplotlib 创建绘图。我必须用不同的颜色来标识每个地图,应该由Python自动生成。

I am using matplotlib to create the plots. I have to identify each plot with a different color which should be automatically generated by Python.

你能给我一个方法,为不同的地块放置不同的颜色数字?

Can you please give me a method to put different colors for different plots in the same figure?

推荐答案

Matplotlib默认执行此操作。

Matplotlib does this by default.

p>

E.g.:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

plt.plot(x, x)
plt.plot(x, 2 * x)
plt.plot(x, 3 * x)
plt.plot(x, 4 * x)
plt.show()

而且,正如你可能已经知道,您可以轻松添加图例:

And, as you may already know, you can easily add a legend:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

plt.plot(x, x)
plt.plot(x, 2 * x)
plt.plot(x, 3 * x)
plt.plot(x, 4 * x)

plt.legend(['y = x', 'y = 2x', 'y = 3x', 'y = 4x'], loc='upper left')

plt.show()


b $ b

如果你想控制将循环的颜色:

If you want to control the colors that will be cycled through:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)

plt.gca().set_color_cycle(['red', 'green', 'blue', 'yellow'])

plt.plot(x, x)
plt.plot(x, 2 * x)
plt.plot(x, 3 * x)
plt.plot(x, 4 * x)

plt.legend(['y = x', 'y = 2x', 'y = 3x', 'y = 4x'], loc='upper left')

plt.show()

希望有所帮助!如果您不熟悉matplotlib,请该教程是一个很好的起点

Hope that helps a bit! If you're unfamiliar with matplotlib, the tutorial is a good place to start.

编辑

首先,如果你有很多)


  1. 将它们放在不同的图上(考虑在一个图上使用几个子图) ,或

  2. 使用颜色以外的其他方式(即标记样式或线条粗细)来区分它们。

否则,你将会遇到一个非常凌乱的情节!很高兴谁会去读任何你在做什么,不要试图把15种不同的东西挤在一个数字!

Otherwise, you're going to wind up with a very messy plot! Be nice to who ever is going to read whatever you're doing and don't try to cram 15 different things onto one figure!!

除此之外,许多人都是不同程度的色盲,区分不同的细微不同的颜色对于更多的人来说是难以实现的。

Beyond that, many people are colorblind to varying degrees, and distinguishing between numerous subtly different colors is difficult for more people than you may realize.

有人说,如果你真的想在一个轴上用20种相对不同的颜色设置20行,这里有一种方法:

That having been said, if you really want to put 20 lines on one axis with 20 relatively distinct colors, here's one way to do it:

import matplotlib.pyplot as plt
import numpy as np

num_plots = 20

# Have a look at the colormaps here and decide which one you'd like:
# http://matplotlib.org/1.2.1/examples/pylab_examples/show_colormaps.html
colormap = plt.cm.gist_ncar
plt.gca().set_color_cycle([colormap(i) for i in np.linspace(0, 0.9, num_plots)])

# Plot several different functions...
x = np.arange(10)
labels = []
for i in range(1, num_plots + 1):
    plt.plot(x, i * x + 5 * i)
    labels.append(r'$y = %ix + %i$' % (i, 5*i))

# I'm basically just demonstrating several different legend options here...
plt.legend(labels, ncol=4, loc='upper center', 
           bbox_to_anchor=[0.5, 1.1], 
           columnspacing=1.0, labelspacing=0.0,
           handletextpad=0.0, handlelength=1.5,
           fancybox=True, shadow=True)

plt.show()

这篇关于如何在一个图中得到不同地块的不同颜色的线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆