amueller / word_cloud

A little word cloud generator in Python
https://amueller.github.io/word_cloud
MIT License
10.13k stars 2.32k forks source link

WordCloud results in a white background instead of a transparent one when when mode is “RGBA” and background_color is None. #569

Open cameronaaron opened 4 years ago

cameronaaron commented 4 years ago

I am working on a project and I need the background of the word cloud to be transparent however even when mode is “RGBA” and background_color is None it results in a white background opposed to a transparent one. I also tried background_color="rgba(255, 255, 255, 0)", mode="RGBA" and get the same result. It needs to be transparent so that the words look like they are floating and you can see through it.

Steps/Code to Reproduce

from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt text = genes_to_display.summary.values wordcloud = WordCloud( width = 800, height = 600, background_color="rgba(255, 255, 255, 0)", mode="RGBA", stopwords = STOPWORDS).generate(str(text)) fig = plt.figure( figsize = (90, 80), edgecolor = 'k') plt.imshow(wordcloud, interpolation = 'bilinear') plt.axis('off') plt.tight_layout(pad=0) plt.show() plt.savefig("wordcloud.png")

and from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt text = genes_to_display.summary.values wordcloud = WordCloud( width = 800, height = 600, mode = 'RGBA', background_color = None, stopwords = STOPWORDS).generate(str(text)) fig = plt.figure( figsize = (90, 80), edgecolor = 'k') plt.imshow(wordcloud, interpolation = 'bilinear') plt.axis('off') plt.tight_layout(pad=0) plt.show() plt.savefig("wordcloud.png")

Expected Results

The background should be transparent, not white

Actual Results

I am given a white background opposed to a transparent one

Versions

Python 3.7.7 (default, Mar 10 2020, 15:43:33) [Clang 11.0.0 (clang-1100.0.33.17)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import platform; print(platform.platform()) Darwin-19.5.0-x86_64-i386-64bit

import sys; print("Python", sys.version) Python 3.7.7 (default, Mar 10 2020, 15:43:33) [Clang 11.0.0 (clang-1100.0.33.17)] import numpy; print("NumPy", numpy.version) NumPy 1.18.1 import matplotlib; print("matplotlib", matplotlib.version) matplotlib 3.2.0 import wordcloud; print("wordcoud", wordcloud.version) wordcoud 1.7.0

cameronaaron commented 4 years ago

Could be similar to this https://github.com/amueller/word_cloud/issues/186 but the solutions that worked for him do not work for me.

amueller commented 4 years ago

You're not saving the wordcloud, but instead you're saving the figure generated by matplotlib, which has white background. Try wordcloud.to_file("wordcloud.png")

jfranc06 commented 4 years ago

You're not saving the wordcloud, but instead you're saving the figure generated by matplotlib, which has white background. Try wordcloud.to_file("wordcloud.png")

Is there any way to render a transparent output using plt.show() so that it can be copy/pasted with bg transparency? (without having to save to file)

amueller commented 4 years ago

I'm not sure if that's possible with matplotlib, though it could be with the gui interface. Are you using jupyter, and what matplotlib magic are you using? With to_image it should be possible, though, and with to_svg it should be possible as well, though I'm failing to make that work right now.

Closing this as the original issue is solved, feel free to open a new issue if to_image doesn't work for you.