日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python:wordcloud.wordcloud()函数的参数解析及其说明

發(fā)布時間:2025/3/21 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python:wordcloud.wordcloud()函数的参数解析及其说明 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Python:wordcloud.wordcloud()函數(shù)的參數(shù)解析及其說明

?

?

目錄

wordcloud.wordcloud()函數(shù)的參數(shù)解析及其說明


?

?

wordcloud.wordcloud()函數(shù)的參數(shù)解析及其說明

class WordCloud Found at: wordcloud.wordcloudclass WordCloud(object):
? ? """Word cloud object for generating and drawing.
? ??
? ? Parameters
? ? ----------
? ? font_path: string
? ? Font path to the font that will be used (OTF or TTF).
? ? Defaults to DroidSansMono path on a Linux machine. If you are on another OS or don't have this font, you need to adjust this path.
? ??
? ? width : int (default=400)
? ? Width of the canvas.
? ??
? ? height : int (default=200)
? ? Height of the canvas.
? ??
? ? prefer_horizontal : float (default=0.90)
? ? The ratio of times to try horizontal fitting as opposed to vertical.??If prefer_horizontal < 1, the algorithm will try rotating the word?? if it doesn't fit. (There is currently no built-in way to get only vertical words.)
? ??
? ? mask : nd-array or None (default=None)
? ? If not None, gives a binary mask on where to draw words. If mask? is not??None, width and height will be ignored and the shape of mask? will be used instead. All white (#FF or #FFFFFF) entries will be considerd?? "masked out" while other entries will be free to draw on. [This??changed in the most recent version!]
? ??
? ? scale : float (default=1)
? ? Scaling between computation and drawing. For large word-cloud???images,
? ? using scale instead of larger canvas size is significantly faster, but might lead to a coarser fit for the words.
? ??
? ? min_font_size : int (default=4)
? ? Smallest font size to use. Will stop when there is no more room???in this??size.
? ??
? ? font_step : int (default=1)
? ? Step size for the font. font_step > 1 might speed up computation? but?? give a worse fit.
? ??
? ? max_words : number (default=200)
? ? The maximum number of words.
? ??
? ? stopwords : set of strings or None
? ? The words that will be eliminated. If None, the build-in? STOPWORDS??list will be used.
? ??
? ? background_color : color value (default="black")
? ? Background color for the word cloud image.
? ??
? ? max_font_size : int or None (default=None)
? ? Maximum font size for the largest word. If None, height of the??? image is used.
? ??
? ? mode : string (default="RGB")
? ? Transparent background will be generated when mode is "RGBA"??and??background_color is None.
? ??
? ? relative_scaling : float (default=.5)
? ? Importance of relative word frequencies for font-size. ?With??relative_scaling=0, only word-ranks are considered. ?With?? relative_scaling=1, a word that is twice as frequent will have twice the size. ?If you want to consider the word frequencies and not? only??their rank, relative_scaling around .5 often looks good.
? ??
? ? .. versionchanged: 2.0
? ? Default is now 0.5.
? ??
? ? color_func: callable, default=None
? ? Callable with parameters word, font_size, position, orientation,??font_path, random_state that returns a PIL color for each word.
? ? Overwrites "colormap". See colormap for specifying a matplotlib colormap instead.
? ??
? ? regexp : string or None (optional)
? ? Regular expression to split the input text into tokens in???process_text.
? ? If None is specified, ``r"\w[\w']+"`` is used.
? ??
? ? collocations : bool, default=True
? ? Whether to include collocations (bigrams) of two words.
? ??
? ? .. versionadded: 2.0
? ??
? ? colormap : string or matplotlib colormap, default="viridis"
? ? Matplotlib colormap to randomly draw colors from for each???word.
? ? Ignored if "color_func" is specified.
? ??
? ? .. versionadded: 2.0
? ??
? ? normalize_plurals : bool, default=True
? ? Whether to remove trailing 's' from words. If True and a word appears with and without a trailing 's', the one with trailing 's'??is removed and its counts are added to the version without??trailing 's' -- unless the word ends with 'ss'.
? ??

類WordCloud在:WordCloud找到。wordcloudclass WordCloud(對象):
用于生成和繪制的Word云對象。

參數(shù)
----------
font_path:字符串
要使用的字體(OTF或TTF)的字體路徑。
Linux機器上的默認DroidSansMono路徑。如果你在另一個操作系統(tǒng)上或者沒有這個字體,你需要調(diào)整這個路徑。

width :int(默認=400)
畫布的寬度。

height :int(默認=200)
畫布的高度。

prefer_horizontal : float(默認=0.90)
嘗試水平擬合與垂直擬合的時間比。如果prefer_horizontal < 1,算法將嘗試旋轉(zhuǎn)不適合的單詞。(目前還沒有內(nèi)置的方法來只獲取垂直的單詞。)

mask : nd-array或None(默認=None)
如果沒有,給出一個二進制掩碼在哪里繪制單詞。如果遮罩不是None,寬度和高度將被忽略,而使用遮罩的形狀。所有白色(#FF或#FFFFFF)的參賽作品將被視為“屏蔽”,而其他參賽作品將可以自由提取。[這在最近的版本中有所改變!]

scale :浮動(默認=1)
在計算和繪圖之間縮放。對于大的字云圖像,
使用scale而不是更大的畫布尺寸會快得多,但可能會導致適合文字的粗化。

min_font_size : int(默認=4)
使用的最小字體大小。將停止時,沒有更多的空間在這個大小。

font_step : int(默認=1)
字體的步長。font_step > 1可能會加速計算,但是匹配效果更差。

max_words :數(shù)字(默認=200)
單詞的最大數(shù)量

stopwords :一組字符串或沒有
將被刪除的單詞。如果沒有,將使用內(nèi)置的STOPWORDS列表

background_color :顏色值(默認=“黑色”)
背景色為字云圖像

max_font_size : int或None(默認=None)
為最大的字的最大字體大小。如果沒有,則使用圖像的高度。

mode :string(默認="RGB")
當模式為“RGBA”,background_color為None時,將生成透明背景。

relative_scaling :浮動(默認= 5)
字體大小的相對頻率的重要性。對于relative_scaling=0,只考慮單詞的等級。使用relative_scaling=1,出現(xiàn)頻率兩倍的單詞的大小也會增加一倍。如果您想要考慮單詞的頻率而不僅僅是它們的排名,那么在5左右的relative_scaling通常看起來不錯。

. .versionchanged: 2.0
現(xiàn)在默認值是0.5。

color_func:可調(diào)用,默認=無
可調(diào)用參數(shù)word, font_size, position, orientation, font_path, random_state,為每個單詞返回一個PIL顏色。
覆蓋“colormap”。請參閱colormap以指定matplotlib的colormap。

regexp :字符串或無(可選)
正則表達式,用于在process_text中將輸入文本分割為令牌。
如果沒有指定,“r”\ w (\ w) +”“使用。
&
collocations :bool, default=True
是否包含兩個單詞的搭配(雙字母組合)

. .versionadded: 2.0

colormap : string或matplotlib colormap,默認="viridis"
Matplotlib colormap為每個單詞隨機繪制顏色。
如果指定了“color_func”,則忽略。

. .versionadded: 2.0

normalize_plurals : bool, default=True
是否刪除單詞后面的“s”。如果是真的,并且一個單詞出現(xiàn)時帶有或不帶有結(jié)尾s,那么帶有結(jié)尾s的單詞將被刪除,并將其計數(shù)添加到?jīng)]有結(jié)尾s的版本中——除非這個單詞以“ss”結(jié)尾。
? ? Attributes
? ? ----------
? ? ``words_`` : dict of string to float
? ? Word tokens with associated frequency.
? ??
? ? .. versionchanged: 2.0
? ? ``words_`` is now a dictionary
? ??
? ? ``layout_ `` : list of tuples (string, int, (int, int), int, color))
? ? Encodes the fitted word cloud. Encodes for each word the string,???font size, position, orientation and color.
? ??
? ? Notes
? ? -----
? ? Larger canvases with make the code significantly slower. If you???need a??large word cloud, try a lower canvas size, and set the scale? parameter.
? ??
? ? The algorithm might give more weight to the ranking of the words??than their actual frequencies, depending on the ``max_font_size `???and the scaling heuristic.
? ? """
屬性
---------
' ' words_ ' ':浮動字符串的dict
具有相關頻率的單詞標記。

. .versionchanged: 2.0
words_”現(xiàn)在是一本字典

' ' layout_ ' ':元組列表(字符串,int, (int, int), int, color))
編碼合適的詞云。為每個單詞編碼字符串、字體大小、位置、方向和顏色。

筆記
-----
較大的畫布使代碼明顯地變慢。如果你需要一個大的字云,嘗試一個較低的畫布大小,并設置比例參數(shù)。

根據(jù)' ' max_font_size '和縮放啟發(fā)式,算法可能給予單詞的排名比它們的實際頻率更多的權重。
”“”

? ? def __init__(self, font_path=None, width=400, height=200,?
? ? ?margin=2,?
? ? ? ? ranks_only=None, prefer_horizontal=.9, mask=None, scale=1,?
? ? ? ? color_func=None, max_words=200, min_font_size=4,?
? ? ? ? stopwords=None, random_state=None,?
? ? ? ? ?background_color='black',?
? ? ? ? max_font_size=None, font_step=1, mode="RGB",?
? ? ? ? relative_scaling=.5, regexp=None, collocations=True,?
? ? ? ? colormap=None, normalize_plurals=True):
? ? ? ? if font_path is None:
? ? ? ? ? ? font_path = FONT_PATH
? ? ? ? if color_func is None and colormap is None:
? ? ? ? ? ? # we need a color map
? ? ? ? ? ? import matplotlib
? ? ? ? ? ? version = matplotlib.__version__
? ? ? ? ? ? if version[0] < "2" and version[2] < "5":
? ? ? ? ? ? ? ? colormap = "hsv"
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? colormap = "viridis"
? ? ? ? self.colormap = colormap
? ? ? ? self.collocations = collocations
? ? ? ? self.font_path = font_path
? ? ? ? self.width = width
? ? ? ? self.height = height
? ? ? ? self.margin = margin
? ? ? ? self.prefer_horizontal = prefer_horizontal
? ? ? ? self.mask = mask
? ? ? ? self.scale = scale
? ? ? ? self.color_func = color_func or colormap_color_func(colormap)
? ? ? ? self.max_words = max_words
? ? ? ? self.stopwords = stopwords if stopwords is not None else?
? ? ? ? ?STOPWORDS
? ? ? ? self.min_font_size = min_font_size
? ? ? ? self.font_step = font_step
? ? ? ? self.regexp = regexp
? ? ? ? if isinstance(random_state, int):
? ? ? ? ? ? random_state = Random(random_state)
? ? ? ? self.random_state = random_state
? ? ? ? self.background_color = background_color
? ? ? ? self.max_font_size = max_font_size
? ? ? ? self.mode = mode
? ? ? ? if relative_scaling < 0 or relative_scaling > 1:
? ? ? ? ? ? raise ValueError(
? ? ? ? ? ? ? ? "relative_scaling needs to be "
? ? ? ? ? ? ? ? "between 0 and 1, got %f." %?
? ? ? ? ? ? ? ? relative_scaling)
? ? ? ? self.relative_scaling = relative_scaling
? ? ? ? if ranks_only is not None:
? ? ? ? ? ? warnings.warn("ranks_only is deprecated and will be?
? ? ? ? ? ? ?removed as"
? ? ? ? ? ? ? ? " it had no effect. Look into relative_scaling.",?
? ? ? ? ? ? ? ? DeprecationWarning)
? ? ? ? self.normalize_plurals = normalize_plurals
? ??
? ? def fit_words(self, frequencies):
? ? ? ? """Create a word_cloud from words and frequencies.

? ? ? ? Alias to generate_from_frequencies.

? ? ? ? Parameters
? ? ? ? ----------
? ? ? ? frequencies : dict from string to float
? ? ? ? ? ? A contains words and associated frequency.

? ? ? ? Returns
? ? ? ? -------
? ? ? ? self
? ? ? ? """
? ? ? ? return self.generate_from_frequencies(frequencies)
? ??
? ? def generate_from_frequencies(self, frequencies,?
? ? ?max_font_size=None):
? ? ? ? """Create a word_cloud from words and frequencies. Parameters

? ? ? ? ----------
? ? ? ? frequencies : dict from string to float
? ? ? ? ? ? A contains words and associated frequency.

? ? ? ? max_font_size : int
? ? ? ? ? ? Use this font-size instead of self.max_font_size

? ? ? ? Returns
? ? ? ? -------
? ? ? ? self

? ? ? ? """
? ? ? ? # make sure frequencies are sorted and normalized
? ? ? ? frequencies = sorted(frequencies.items(), key=itemgetter(1),?
? ? ? ? ?reverse=True)
? ? ? ? if len(frequencies) <= 0:
? ? ? ? ? ? raise ValueError("We need at least 1 word to plot a word?
? ? ? ? ? ? ?cloud, "
? ? ? ? ? ? ? ? "got %d." %?
? ? ? ? ? ? ? ? len(frequencies))
? ? ? ? frequencies = frequencies[:self.max_words] # largest entry will?
? ? ? ? ?be 1
? ? ? ? max_frequency = float(frequencies[0][1])
? ? ? ? frequencies = [(word, freq / max_frequency) for?
? ? ? ? ? ? word, freq in frequencies]
? ? ? ? if self.random_state is not None:
? ? ? ? ? ? random_state = self.random_state
? ? ? ? else:
? ? ? ? ? ? random_state = Random()
? ? ? ? if self.mask is not None:
? ? ? ? ? ? mask = self.mask
? ? ? ? ? ? width = mask.shape[1]
? ? ? ? ? ? height = mask.shape[0]
? ? ? ? ? ? if mask.dtype.kind == 'f':
? ? ? ? ? ? ? ? warnings.warn("mask image should be unsigned byte?
? ? ? ? ? ? ? ? ?between 0"
? ? ? ? ? ? ? ? ? ? " and 255. Got a float array")
? ? ? ? ? ? if mask.ndim == 2:
? ? ? ? ? ? ? ? boolean_mask = mask == 255
? ? ? ? ? ? elif mask.ndim == 3: # if all channels are white, mask out
? ? ? ? ? ? ? ? :::3]255, axis=-1)
? ? ? ? else:
? ? ? ? ? ? boolean_mask = np.all(mask[ ==?
? ? ? ? ? ? ? ? raise ValueError("Got mask of invalid shape: %s" %?
? ? ? ? ? ? ? ? ? ? str(mask.shape))
? ? ? ? else:
? ? ? ? ? ? boolean_mask = None
? ? ? ? ? ? height, width = self.height, self.width
? ? ? ? occupancy = IntegralOccupancyMap(height, width,?
? ? ? ? ?boolean_mask)
? ? ? ? # create image
? ? ? ? img_grey = Image.new("L", (width, height))
? ? ? ? draw = ImageDraw.Draw(img_grey)
? ? ? ? img_array = np.asarray(img_grey)
? ? ? ? font_sizes, positions, orientations, colors = [], [], [], []
? ? ? ? last_freq = 1.
? ? ? ? if max_font_size is None:
? ? ? ? ? ? # if not provided use default font_size
? ? ? ? ? ? max_font_size = self.max_font_size
? ? ? ? if max_font_size is None:
? ? ? ? ? ? # figure out a good font size by trying to draw with
? ? ? ? ? ? # just the first two words
? ? ? ? ? ? if len(frequencies) == 1:
? ? ? ? ? ? ? ? # we only have one word. We make it big!
? ? ? ? ? ? ? ? font_size = self.height
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? self.generate_from_frequencies(dict(frequencies[:2]),?
? ? ? ? ? ? ? ? ? ? max_font_size=self.height)
? ? ? ? ? ? ? ? # find font sizes
? ? ? ? ? ? ? ? sizes = [x[1] for x in self.layout_]
? ? ? ? ? ? ? ? try:
? ? ? ? ? ? ? ? ? ? font_size = int(2 * sizes[0] * sizes[1] /?
? ? ? ? ? ? ? ? ? ? ? ? (sizes[0] + sizes[1]))
? ? ? ? ? ? ? ? # quick fix for if self.layout_ contains less than 2 values
? ? ? ? ? ? ? ? # on very small images it can be empty
? ? ? ? ? ? ? ? except IndexError:
? ? ? ? ? ? ? ? ? ? try:
? ? ? ? ? ? ? ? ? ? ? ? font_size = sizes[0]
? ? ? ? ? ? ? ? ? ? except IndexError:
? ? ? ? ? ? ? ? ? ? ? ? raise ValueError('canvas size is too small')
? ? ? ? else:
? ? ? ? ? ? font_size = max_font_size
? ? ? ? # we set self.words_ here because we called?
? ? ? ? ?generate_from_frequencies
? ? ? ? # above... hurray for good design?
? ? ? ? self.words_ = dict(frequencies)
? ? ? ? # start drawing grey image
? ? ? ? for word, freq in frequencies:
? ? ? ? ? ? # select the font size
? ? ? ? ? ? rs = self.relative_scaling
? ? ? ? ? ? if rs != 0:
? ? ? ? ? ? ? ? font_size = int(round((rs * (freq / float(last_freq)) +?
? ? ? ? ? ? ? ? ? ? ? ? ? ? (1 - rs)) * font_size))
? ? ? ? ? ? if random_state.random() < self.prefer_horizontal:
? ? ? ? ? ? ? ? orientation = None
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? orientation = Image.ROTATE_90
? ? ? ? ? ? tried_other_orientation = False
? ? ? ? ? ? while True:
? ? ? ? ? ? ? ? # try to find a position
? ? ? ? ? ? ? ? font = ImageFont.truetype(self.font_path, font_size)
? ? ? ? ? ? ? ? # transpose font optionally
? ? ? ? ? ? ? ? transposed_font = ImageFont.TransposedFont(
? ? ? ? ? ? ? ? ? ? font, orientation=orientation)
? ? ? ? ? ? ? ? # get size of resulting text
? ? ? ? ? ? ? ? box_size = draw.textsize(word, font=transposed_font)
? ? ? ? ? ? ? ? # find possible places using integral image:
? ? ? ? ? ? ? ? result = occupancy.sample_position(box_size[1] + self.
? ? ? ? ? ? ? ? ?margin,?
? ? ? ? ? ? ? ? ? ? box_size[0] + self.margin,?
? ? ? ? ? ? ? ? ? ? random_state)
? ? ? ? ? ? ? ? if result is not None or font_size < self.min_font_size:
? ? ? ? ? ? ? ? ? ? # either we found a place or font-size went too small
? ? ? ? ? ? ? ? ? ? break
? ? ? ? ? ? ? ? # if we didn't find a place, make font smaller
? ? ? ? ? ? ? ? # but first try to rotate!
? ? ? ? ? ? ? ? if not tried_other_orientation and self.prefer_horizontal <?
? ? ? ? ? ? ? ? ?1:
? ? ? ? ? ? ? ? ? ? orientation = Image.ROTATE_90 if orientation is None?
? ? ? ? ? ? ? ? ? ? ?else Image.ROTATE_90
? ? ? ? ? ? ? ? ? ? tried_other_orientation = True
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? font_size -= self.font_step
? ? ? ? ? ? ? ? ? ? orientation = None
? ? ? ? ? ??
? ? ? ? ? ? if font_size < self.min_font_size:
? ? ? ? ? ? ? ? # we were unable to draw any more
? ? ? ? ? ? ? ? break
? ? ? ? ? ? x, y = np.array(result) + self.margin // 2
? ? ? ? ? ? # actually draw the text
? ? ? ? ? ? draw.text((y, x), word, fill="white", font=transposed_font)
? ? ? ? ? ? positions.append((x, y))
? ? ? ? ? ? orientations.append(orientation)
? ? ? ? ? ? font_sizes.append(font_size)
? ? ? ? ? ? colors.append(self.color_func(word, font_size=font_size,?
? ? ? ? ? ? ? ? ? ? position=(x, y),?
? ? ? ? ? ? ? ? ? ? orientation=orientation,?
? ? ? ? ? ? ? ? ? ? random_state=random_state,?
? ? ? ? ? ? ? ? ? ? font_path=self.font_path))
? ? ? ? ? ? # recompute integral image
? ? ? ? ? ? if self.mask is None:
? ? ? ? ? ? ? ? img_array = np.asarray(img_grey)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? img_array = np.asarray(img_grey) + boolean_mask
? ? ? ? ? ? # recompute bottom right
? ? ? ? ? ? # the order of the cumsum's is important for speed ?!
? ? ? ? ? ? occupancy.update(img_array, x, y)
? ? ? ? ? ? last_freq = freq
? ? ? ??
? ? ? ? self.layout_ = list(zip(frequencies, font_sizes, positions,?
? ? ? ? ? ? ? ? orientations, colors))
? ? ? ? return self
? ??
? ? def process_text(self, text):
? ? ? ? """Splits a long text into words, eliminates the stopwords.

? ? ? ? Parameters
? ? ? ? ----------
? ? ? ? text : string
? ? ? ? ? ? The text to be processed.

? ? ? ? Returns
? ? ? ? -------
? ? ? ? words : dict (string, int)
? ? ? ? ? ? Word tokens with associated frequency.

? ? ? ? ..versionchanged:: 1.2.2
? ? ? ? ? ? Changed return type from list of tuples to dict.

? ? ? ? Notes
? ? ? ? -----
? ? ? ? There are better ways to do word tokenization, but I don't?
? ? ? ? ?want to
? ? ? ? include all those things.
? ? ? ? """
? ? ? ? stopwords = set([i.lower() for i in self.stopwords])
? ? ? ? flags = re.UNICODE if sys.version < '3' and type(text) is unicode?
? ? ? ? ?else 0
? ? ? ? regexp = self.regexp if self.regexp is not None else r"\w[\w']+"
? ? ? ? words = re.findall(regexp, text, flags)
? ? ? ? # remove stopwords
? ? ? ? words = [word for word in words if word.lower() not in?
? ? ? ? ?stopwords]
? ? ? ? # remove 's
? ? ? ? words = [word[:-2] if word.lower().endswith("'s") else word for?
? ? ? ? ? ? word in words]
? ? ? ? # remove numbers
? ? ? ? words = [word for word in words if not word.isdigit()]
? ? ? ? if self.collocations:
? ? ? ? ? ? word_counts = unigrams_and_bigrams(words, self.
? ? ? ? ? ? ?normalize_plurals)
? ? ? ? else:
? ? ? ? ? ? word_counts, _ = process_tokens(words, self.
? ? ? ? ? ? ?normalize_plurals)
? ? ? ? return word_counts
? ??
? ? def generate_from_text(self, text):
? ? ? ? """Generate wordcloud from text.

? ? ? ? The input "text" is expected to be a natural text. If you pass a?
? ? ? ? ?sorted
? ? ? ? list of words, words will appear in your output twice. To?
? ? ? ? ?remove this
? ? ? ? duplication, set ``collocations=False``.

? ? ? ? Calls process_text and generate_from_frequencies.

? ? ? ? ..versionchanged:: 1.2.2
? ? ? ? ? ? Argument of generate_from_frequencies() is not return of
? ? ? ? ? ? process_text() any more.

? ? ? ? Returns
? ? ? ? -------
? ? ? ? self
? ? ? ? """
? ? ? ? words = self.process_text(text)
? ? ? ? self.generate_from_frequencies(words)
? ? ? ? return self
? ??
? ? def generate(self, text):
? ? ? ? """Generate wordcloud from text.

? ? ? ? The input "text" is expected to be a natural text. If you pass a?
? ? ? ? ?sorted
? ? ? ? list of words, words will appear in your output twice. To?
? ? ? ? ?remove this
? ? ? ? duplication, set ``collocations=False``.

? ? ? ? Alias to generate_from_text.

? ? ? ? Calls process_text and generate_from_frequencies.

? ? ? ? Returns
? ? ? ? -------
? ? ? ? self
? ? ? ? """
? ? ? ? return self.generate_from_text(text)
? ??
? ? def _check_generated(self):
? ? ? ? """Check if ``layout_`` was computed, otherwise raise error."""
? ? ? ? if not hasattr(self, "layout_"):
? ? ? ? ? ? raise ValueError("WordCloud has not been calculated, call?
? ? ? ? ? ? ?generate"
? ? ? ? ? ? ? ? " first.")
? ??
? ? def to_image(self):
? ? ? ? self._check_generated()
? ? ? ? if self.mask is not None:
? ? ? ? ? ? width = self.mask.shape[1]
? ? ? ? ? ? height = self.mask.shape[0]
? ? ? ? else:
? ? ? ? ? ? height, width = self.height, self.width
? ? ? ? img = Image.new(self.mode, (int(width * self.scale),?
? ? ? ? ? ? ? ? int(height * self.scale)),?
? ? ? ? ? ? self.background_color)
? ? ? ? draw = ImageDraw.Draw(img)
? ? ? ? for (word, count), font_size, position, orientation, color in self.
? ? ? ? ?layout_:
? ? ? ? ? ? font = ImageFont.truetype(self.font_path,?
? ? ? ? ? ? ? ? int(font_size * self.scale))
? ? ? ? ? ? transposed_font = ImageFont.TransposedFont(
? ? ? ? ? ? ? ? font, orientation=orientation)
? ? ? ? ? ? pos = int(position[1] * self.scale), int(position[0] * self.scale)
? ? ? ? ? ? draw.text(pos, word, fill=color, font=transposed_font)
? ? ? ??
? ? ? ? return img
? ??
? ? def recolor(self, random_state=None, color_func=None,?
? ? ?colormap=None):
? ? ? ? """Recolor existing layout.

? ? ? ? Applying a new coloring is much faster than generating the?
? ? ? ? ?whole
? ? ? ? wordcloud.

? ? ? ? Parameters
? ? ? ? ----------
? ? ? ? random_state : RandomState, int, or None, default=None
? ? ? ? ? ? If not None, a fixed random state is used. If an int is given,?
? ? ? ? ? ? ?this
? ? ? ? ? ? is used as seed for a random.Random state.

? ? ? ? color_func : function or None, default=None
? ? ? ? ? ? Function to generate new color from word count, font size,?
? ? ? ? ? ? ?position
? ? ? ? ? ? and orientation. ?If None, self.color_func is used.

? ? ? ? colormap : string or matplotlib colormap, default=None
? ? ? ? ? ? Use this colormap to generate new colors. Ignored if?
? ? ? ? ? ? ?color_func
? ? ? ? ? ? is specified. If None, self.color_func (or self.color_map) is?
? ? ? ? ? ? ?used.

? ? ? ? Returns
? ? ? ? -------
? ? ? ? self
? ? ? ? """
? ? ? ? if isinstance(random_state, int):
? ? ? ? ? ? random_state = Random(random_state)
? ? ? ? self._check_generated()
? ? ? ? if color_func is None:
? ? ? ? ? ? if colormap is None:
? ? ? ? ? ? ? ? color_func = self.color_func
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? color_func = colormap_color_func(colormap)
? ? ? ? self.layout_ = [(word_freq, font_size, position, orientation,?
? ? ? ? ? ? ? ? color_func(word=word_freq[0], font_size=font_size,?
? ? ? ? ? ? ? ? ? ? position=position, orientation=orientation,?
? ? ? ? ? ? ? ? ? ? random_state=random_state,?
? ? ? ? ? ? ? ? ? ? font_path=self.font_path)) for?
? ? ? ? ? ? word_freq, font_size, position, orientation, _ in?
? ? ? ? ? ? self.layout_]
? ? ? ? return self
? ??
? ? def to_file(self, filename):
? ? ? ? """Export to image file.

? ? ? ? Parameters
? ? ? ? ----------
? ? ? ? filename : string
? ? ? ? ? ? Location to write to.

? ? ? ? Returns
? ? ? ? -------
? ? ? ? self
? ? ? ? """
? ? ? ? img = self.to_image()
? ? ? ? img.save(filename, optimize=True)
? ? ? ? return self
? ??
? ? def to_array(self):
? ? ? ? """Convert to numpy array.

? ? ? ? Returns
? ? ? ? -------
? ? ? ? image : nd-array size (width, height, 3)
? ? ? ? ? ? Word cloud image as numpy matrix.
? ? ? ? """
? ? ? ? return np.array(self.to_image())
? ??
? ? def __array__(self):
? ? ? ? """Convert to numpy array.

? ? ? ? Returns
? ? ? ? -------
? ? ? ? image : nd-array size (width, height, 3)
? ? ? ? ? ? Word cloud image as numpy matrix.
? ? ? ? """
? ? ? ? return self.to_array()
? ??
? ? def to_html(self):
? ? ? ? raise NotImplementedError("FIXME!!!")

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的Python:wordcloud.wordcloud()函数的参数解析及其说明的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。