如何在 NLTK Matplotlib 函数中设置多条线的颜色?
How can I set the colors for multiple lines in an NLTK Matplotlib function?
我有一个 NLTK 函数可以创建这样的 AxesSubplot:
# Names ending letters frequency
import nltk
import matplotlib.pyplot as plt
cfd = nltk.ConditionalFreqDist(
(fileid, name[-1])
for fileid in names.fileids()
for name in names.words(fileid))
plt.figure(figsize=(12, 6))
cfd.plot()
而且我想单独更改线条的颜色。我在网上看到的大多数解决方案都使用单独的绘图线单独生成每一行。然而,在 ConditionalFreqDist .plot()
中调用了 matplotlib .plot()
方法。有没有其他方法可以更改线条的颜色?我想要母线是蓝色,公线是绿色。
NLTK 的 ConditionalFreqDist.plot
方法 returns 一个普通的 matplotlib axes
对象,如您所见 here。由此,您可以直接使用 ax.lines
获取线条并使用 set_color
.
设置颜色
我现在没有安装 NLTK,所以我直接制作坐标轴,绘制一条红线和一条蓝线,然后将它们变成黑色和绿色。
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 15, 21)
y0 = 0.6*np.sin(x)
y1 = np.sin(1.2 + 0.5*x)
fig, ax = plt.subplots(1,1)
ax.plot(x, y0, 'r')
ax.plot(x, y1, 'b')
# this is where ConditionalFreqDist will return the axes
# these are the lines you would write
ax.lines[0].set_color('k')
ax.lines[1].set_color('g')
具体来说,对于 OP 的情况,它应该如下所示:
import nltk
import matplotlib.pyplot as plt
cfd = nltk.ConditionalFreqDist(
(fileid, name[-1])
for fileid in names.fileids()
for name in names.words(fileid))
plt.figure(figsize=(12, 6))
ax = cfd.plot()
ax.lines[0].set_color('k')
ax.lines[1].set_color('g')
根据@tom10 的建议,这最终成功了:
import nltk
import matplotlib.pyplot as plt
cfd = nltk.ConditionalFreqDist(
(fileid, name[-1])
for fileid in names.fileids()
for name in names.words(fileid))
fig, ax = plt.subplots(1,1)
cfd.plot()
ax.lines[0].set_color('blue')
ax.lines[1].set_color('green')
fig.set_size_inches(10, 4)
fig
我有一个 NLTK 函数可以创建这样的 AxesSubplot:
# Names ending letters frequency
import nltk
import matplotlib.pyplot as plt
cfd = nltk.ConditionalFreqDist(
(fileid, name[-1])
for fileid in names.fileids()
for name in names.words(fileid))
plt.figure(figsize=(12, 6))
cfd.plot()
而且我想单独更改线条的颜色。我在网上看到的大多数解决方案都使用单独的绘图线单独生成每一行。然而,在 ConditionalFreqDist .plot()
中调用了 matplotlib .plot()
方法。有没有其他方法可以更改线条的颜色?我想要母线是蓝色,公线是绿色。
NLTK 的 ConditionalFreqDist.plot
方法 returns 一个普通的 matplotlib axes
对象,如您所见 here。由此,您可以直接使用 ax.lines
获取线条并使用 set_color
.
我现在没有安装 NLTK,所以我直接制作坐标轴,绘制一条红线和一条蓝线,然后将它们变成黑色和绿色。
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 15, 21)
y0 = 0.6*np.sin(x)
y1 = np.sin(1.2 + 0.5*x)
fig, ax = plt.subplots(1,1)
ax.plot(x, y0, 'r')
ax.plot(x, y1, 'b')
# this is where ConditionalFreqDist will return the axes
# these are the lines you would write
ax.lines[0].set_color('k')
ax.lines[1].set_color('g')
具体来说,对于 OP 的情况,它应该如下所示:
import nltk
import matplotlib.pyplot as plt
cfd = nltk.ConditionalFreqDist(
(fileid, name[-1])
for fileid in names.fileids()
for name in names.words(fileid))
plt.figure(figsize=(12, 6))
ax = cfd.plot()
ax.lines[0].set_color('k')
ax.lines[1].set_color('g')
根据@tom10 的建议,这最终成功了:
import nltk
import matplotlib.pyplot as plt
cfd = nltk.ConditionalFreqDist(
(fileid, name[-1])
for fileid in names.fileids()
for name in names.words(fileid))
fig, ax = plt.subplots(1,1)
cfd.plot()
ax.lines[0].set_color('blue')
ax.lines[1].set_color('green')
fig.set_size_inches(10, 4)
fig