树形图 - 更改字体
Treemap - Change Fonts
所以我有以下代码在 Matplotlib 中生成树状图。想知道是否可以将第一行加粗和更大的字体大小。
所以:
底气更大:0:
更小没有底气:480
import matplotlib.pyplot as plt
import squarify # pip install squarify (algorithm for treemap)
import pandas as pd
import psycopg2
distributionlist = [480, 104, 55, 35, 29, 12, 18, 22, 14, 11, 144]
group = ["0", "1", "2", "3", "4", "5", "6", "7","8","9","10+"]
labels = ["%s:\n%s" % (label) for label in zip(group, distributionlist)]
percents = [8,3,4,2,8,3,4,2]
# Create a data frame with fake data
df = pd.DataFrame({'nb_people': distributionlist, 'group':group })
#df = pd.DataFrame({'nb_people':[8,3,4,2], 'group':["group A", "group B", "group C", "group D"] })
# plot it
squarify.plot(sizes=df['nb_people'], label=labels, alpha=.8)
#squarify.plot(sizes=df['nb_people'], label=df['group'], alpha=.8 , color=colors )
plt.axis('off')
plt.show()
通常单个matplotlib文本只有一种尺寸和一种风格。这里标签被创建为一个带有换行符的字符串。
您可以使用
ax = squarify.plot(sizes=df['nb_people'], label=labels, alpha=.8)
ax.texts[0].set_size(30)
ax.texts[0].set_weight('bold')
将文本设置得更大更粗。
拆分文本只设置第一行的粗细:
ax = squarify.plot(sizes=df['nb_people'], label=labels, alpha=.8)
text_parts = ax.texts[0].get_text().split("\n")
ax.texts[0].set_text(text_parts[0] + "\n")
ax.texts[0].set_size(30)
x, y = ax.texts[0].get_position()
new_text = ax.text(x, y, "\n" + text_parts[1], ha=ax.texts[0].get_ha(), va=ax.texts[0].get_va())
new_text.set_fontproperties(ax.texts[0].get_fontproperties())
ax.texts[0].set_weight('bold')
所以我有以下代码在 Matplotlib 中生成树状图。想知道是否可以将第一行加粗和更大的字体大小。
所以:
底气更大:0:
更小没有底气:480
import matplotlib.pyplot as plt
import squarify # pip install squarify (algorithm for treemap)
import pandas as pd
import psycopg2
distributionlist = [480, 104, 55, 35, 29, 12, 18, 22, 14, 11, 144]
group = ["0", "1", "2", "3", "4", "5", "6", "7","8","9","10+"]
labels = ["%s:\n%s" % (label) for label in zip(group, distributionlist)]
percents = [8,3,4,2,8,3,4,2]
# Create a data frame with fake data
df = pd.DataFrame({'nb_people': distributionlist, 'group':group })
#df = pd.DataFrame({'nb_people':[8,3,4,2], 'group':["group A", "group B", "group C", "group D"] })
# plot it
squarify.plot(sizes=df['nb_people'], label=labels, alpha=.8)
#squarify.plot(sizes=df['nb_people'], label=df['group'], alpha=.8 , color=colors )
plt.axis('off')
plt.show()
通常单个matplotlib文本只有一种尺寸和一种风格。这里标签被创建为一个带有换行符的字符串。
您可以使用
ax = squarify.plot(sizes=df['nb_people'], label=labels, alpha=.8)
ax.texts[0].set_size(30)
ax.texts[0].set_weight('bold')
将文本设置得更大更粗。
拆分文本只设置第一行的粗细:
ax = squarify.plot(sizes=df['nb_people'], label=labels, alpha=.8)
text_parts = ax.texts[0].get_text().split("\n")
ax.texts[0].set_text(text_parts[0] + "\n")
ax.texts[0].set_size(30)
x, y = ax.texts[0].get_position()
new_text = ax.text(x, y, "\n" + text_parts[1], ha=ax.texts[0].get_ha(), va=ax.texts[0].get_va())
new_text.set_fontproperties(ax.texts[0].get_fontproperties())
ax.texts[0].set_weight('bold')