Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom and radom Font Size #731

Open
MarleneBs opened this issue Aug 7, 2023 · 0 comments
Open

Custom and radom Font Size #731

MarleneBs opened this issue Aug 7, 2023 · 0 comments

Comments

@MarleneBs
Copy link

I have the following issue: I have an existing code that creates a word cloud, and it works fine. The initial problem is that the sizes of the words are not randomly distributed; the first word I input ends up the largest, and then it decreases in size. However, I'd like the sizes to be randomly assigned. Now, I want to modify my code to set specific font sizes for a few selected words while keeping the rest of the words randomly distributed. I want to ensure that the other functionalities of my code remain intact. In other words, each word should still appear exactly once, and the use of the mask, colors, and font should remain unchanged.

This is my Code:

import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from PIL import Image


text = "Python  Analyse Hallo "
zweiteilige_woerter = {"Hallo DU", "Deep Learning", "Netzszenarien"}

frequencies = {}
for word in text.split():
    frequencies[word] = frequencies.get(word, 0) + 1

# Verarbeite die Wörter aus der Menge der zweiteiligen Wörter und setze ihre Häufigkeiten in frequencies
for word in zweiteilige_woerter:
    frequencies[word] = frequencies.get(word, 0) + 1

# Erstelle eine quadratische Maske in der gewünschten Größe
width_px = 800
height_px = 800
x, y = np.ogrid[:width_px, :height_px]
mask = (x - width_px//2) ** 2 + (y - height_px//2) ** 2 > (width_px//2) ** 2
mask = 255 * mask.astype(int)

wordcloud = WordCloud(background_color=None, mode="RGBA", contour_color='white', contour_width=0, prefer_horizontal=1.0, font_path='arial', mask=mask, max_font_size=80)


wordcloud.generate_from_frequencies(frequencies=frequencies)

colors = [(100, 30, 10), (0, 0, 0)]   #example, I used different colors, but this is just the kind of 


def random_color_func(word=None, font_size=None, position=None, orientation=None, font_path=None, random_state=None):
    return colors[np.random.randint(0, len(colors))]

# Wende die Farben auf die WordCloud an
wordcloud.recolor(color_func=random_color_func)

# Zeige die WordCloud an
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

I tried multiple ways with CHatGBT to change my Code in the way I discribed, but I nothing worked.
Greetings
Marlene

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant