python:做柱状图

import matplotlib.pyplot as plt
# 数据
categories = ['A', 'B', 'C', 'D']
values = [23, 45, 56, 78]
# 创建柱状图
plt.bar(categories, values)
# 添加标题和标签
plt.title('柱状图示例')
plt.xlabel('类别')
plt.ylabel('数值')
# 显示图形
plt.show()

D:\software\新建文件夹\python\Lib\tkinter\__init__.py:861: UserWarning: Glyph 25968 (\N{CJK UNIFIED IDEOGRAPH-6570}) missing from current font.
  func(*args)

出现报错,字体不可用,我们可以看到,设置的题目和x轴y轴的名称没有出现。这时候,我们就要解决问题。

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

# 设置新宋体字体的路径
font_path = 'D:/reportlab/simsun/simsun.ttf'

# 加载新宋体字体
prop = FontProperties(fname=font_path)

# 数据
categories = ['A', 'B', 'C', 'D']
values = [23, 45, 56, 78]

# 创建柱状图
plt.bar(categories, values)

# 添加标题和标签,并使用新宋体字体
plt.title('柱状图示例', fontproperties=prop)
plt.xlabel('类别', fontproperties=prop)
plt.ylabel('数值', fontproperties=prop)

# 设置刻度字体
plt.xticks(fontproperties=prop)
plt.yticks(fontproperties=prop)

# 显示图形
plt.show()

我们设置了宋体,作为图片文字的字体。如何下载宋体,详见python:reportlab 生成pdf:基本用法。-CSDN博客

可我想要图片设置成不同的颜色,该如何是好

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

# 设置新宋体字体的路径
font_path = 'D:/reportlab/simsun/simsun.ttf'

# 加载新宋体字体
prop = FontProperties(fname=font_path)

# 数据
categories = ['A', 'B', 'C', 'D']
values = [23, 45, 56, 78]

# 创建柱状图,并设置颜色
plt.bar(categories, values, color=['red', 'green', 'blue', 'orange'])

# 添加标题和标签,并使用新宋体字体
plt.title('柱状图示例', fontproperties=prop)
plt.xlabel('类别', fontproperties=prop)
plt.ylabel('数值', fontproperties=prop)

# 设置刻度字体
plt.xticks(fontproperties=prop)
plt.yticks(fontproperties=prop)

# 显示图形
plt.show()

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

# 设置新宋体字体的路径
font_path = 'D:/reportlab/simsun/simsun.ttf'

# 加载新宋体字体
prop = FontProperties(fname=font_path)
# 读取 xlsx 文件
df = pd.read_excel('your_excel_file.xlsx')

# 假设 xlsx 文件包含两列,一列是类别,一列是数值
categories = df['Category'].tolist()
values = df['Value'].tolist()

# 创建柱状图,并设置颜色

plt.bar(categories, values, color=['red', 'green', 'blue', 'orange'])#颜色可以增加。
# 添加标题和标签,并使用新宋体字体

plt.title('柱状图示例', fontproperties=prop)
plt.xlabel('类别', fontproperties=prop)
plt.ylabel('数值', fontproperties=prop)

# 设置刻度字体
plt.xticks(fontproperties=prop)
plt.yticks(fontproperties=prop)

# 显示图形
plt.show()

相关推荐

  1. Day8-Python基础学习之地构建

    2024-05-10 09:46:07       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-10 09:46:07       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-10 09:46:07       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-10 09:46:07       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-10 09:46:07       18 阅读

热门阅读

  1. go匿名字段

    2024-05-10 09:46:07       10 阅读
  2. k8s集群的常用命令

    2024-05-10 09:46:07       11 阅读
  3. Django框架之Ajax基础

    2024-05-10 09:46:07       8 阅读
  4. 什么是图片的像素与分辨率?

    2024-05-10 09:46:07       8 阅读
  5. 机器学习过拟合解决方案 - 正则化

    2024-05-10 09:46:07       13 阅读
  6. 数据结构及算法——数组和字符串一些记录

    2024-05-10 09:46:07       11 阅读
  7. 分布式本地缓存刷新-日常笔记

    2024-05-10 09:46:07       8 阅读
  8. zookeeper之分布式环境搭建

    2024-05-10 09:46:07       10 阅读