plt.bar(df['district'],range(len(df['comment'])))
怎么把x轴的汉字变成竖直的而不是横着的,不然全部黏在一起了
plt.xticks(rotation=90)
【以下回答由 GPT 生成】
解决方案:
要将x轴的汉字调整为竖直显示,可以通过设置坐标轴的刻度标签进行实现。请按照以下步骤进行修改:
import matplotlib.pyplot as plt
from matplotlib import font_manager
font_path = "字体文件路径" # 替换为字体文件的完整路径,一般为.ttf格式
font_prop = font_manager.FontProperties(fname=font_path)
plt.xticks(range(len(df['district'])), df['district'], rotation=90, fontproperties=font_prop)
plt.bar(range(len(df['district'])), df['comment'])
完整代码示例:
import matplotlib.pyplot as plt
from matplotlib import font_manager
# 加载字体文件
font_path = "字体文件路径"
font_prop = font_manager.FontProperties(fname=font_path)
# 设置刻度标签的字体属性
plt.xticks(range(len(df['district'])), df['district'], rotation=90, fontproperties=font_prop)
# 绘制柱状图
plt.bar(range(len(df['district'])), df['comment'])
# 显示图形
plt.show()
如果你无法找到适合的字体文件,可以在PyCharm中安装一些支持中文的字体库,例如SimHei
、Microsoft YaHei
等,然后使用对应的字体名称进行设置即可。
希望以上解决方案能够帮助到你,如果还有其他问题,请随时追问。
【相关推荐】