【Python】Pycharm中plot绘图不能显示

本文介绍如何在PyCharm中解决使用Pandas进行数据分析时绘图无法显示的问题。通过引入matplotlib.pyplot库并调用其show()方法,成功实现了绘图的展示。
部署运行你感兴趣的模型镜像

《利用Python进行数据分析》书中例子都是用IPython作为开发环境,问我一直在用Pycharm,这导致了plot还不能显示绘图。出错的代码如下:

import json
import pandas as pd
from pandas import DataFrame,Series

path = r'C:\Users\long\Desktop\python\pydata-book-2nd-edition\datasets\bitly_usagov\example.txt'
records = [json.loads(line) for line in open(path)]
time_zones = [rec['tz'] for rec in records if 'tz' in rec]
frame = DataFrame(records)
clean_tz = frame['tz'].fillna('Missing')
clean_tz[clean_tz == ''] = 'Unknown'
tz_counts = clean_tz.value_counts()

tz_counts[:10].plot(kind='barh',rot=0)

解决方法:导入matplotlib.pyplot库,绘图后再调用matplotlib.pyplot.show()方法就能把绘制的图显示出来了。

修改后代码如下:

import json
import pandas as pd
from pandas import DataFrame,Series
import matplotlib.pyplot as plt

path = r'C:\Users\long\Desktop\python\pydata-book-2nd-edition\datasets\bitly_usagov\example.txt'
records = [json.loads(line) for line in open(path)]
time_zones = [rec['tz'] for rec in records if 'tz' in rec]
frame = DataFrame(records)
clean_tz = frame['tz'].fillna('Missing')
clean_tz[clean_tz == ''] = 'Unknown'
tz_counts = clean_tz.value_counts()
tz_counts[:10].plot(kind='barh',rot=0)

plt.show(tz_counts[:10].plot(kind='barh',rot=0))

您可能感兴趣的与本文相关的镜像

Python3.11

Python3.11

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

PyCharm 中使用 `matplotlib` 绘图时,如果遇到中文无法正常显示的问题,通常是由于 `matplotlib` 默认不支持中文字体导致的。以下是几种有效的解决方法,适用于不同操作系统和环境配置。 ### 方法一:设置中文字体和解决负号显示问题 可以通过修改 `matplotlib` 的全局配置,指定支持中文的字体(如 `SimHei`),并禁用 `unicode_minus` 以正确显示负号: ```python import matplotlib.pyplot as plt # 设置中文字体为 SimHei plt.rcParams['font.sans-serif'] = ['SimHei'] # 解决负号 '-' 显示为方块的问题 plt.rcParams['axes.unicode_minus'] = False # 示例绘图 x = ['北京', '上海', '广州', '深圳'] y = [2154, 2415, 1404, 1567] plt.bar(x, y) plt.xlabel('城市') plt.ylabel('人口(万人)') plt.title('主要城市人口数量') plt.show() ``` ### 方法二:手动安装中文字体并更新缓存(适用于 Linux) 如果你使用的是 Ubuntu 或其他 Linux 系统,可以手动下载并安装 `SimHei` 字体,然后更新 `matplotlib` 的字体缓存。 1. 下载 `SimHei` 字体(可以从 Windows 字体目录中提取 `simhei.ttf`)。 2. 将字体文件复制到 `matplotlib` 的字体目录。 3. 清除字体缓存: ```bash rm -rf ~/.cache/matplotlib ``` 4. 重新运行代码,确保字体生效。 ### 方法三:MacOS 下使用系统支持的中文字体 在 macOS 上,可以使用系统自带的中文字体,如 `Arial Unicode MS`,避免手动安装字体的麻烦: ```python import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] plt.rcParams['axes.unicode_minus'] = False # 示例绘图 x = ['张三', '李四', '王五'] y = [85, 92, 88] plt.bar(x, y) plt.xlabel('姓名') plt.ylabel('成绩') plt.title('学生成绩') plt.show() ``` ### 方法四:永久修改 `matplotlib` 配置文件 如果希望永久解决中文显示问题,可以直接修改 `matplotlib` 的配置文件 `matplotlibrc`,设置默认字体: 1. 查找配置文件路径: ```python import matplotlib print(matplotlib.matplotlib_fname()) ``` 2. 编辑 `matplotlibrc` 文件,修改以下配置项: ``` font.family: sans-serif font.sans-serif: SimHei, Bitstream Vera Sans, DejaVu Sans, Bitstream Vera Sans, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif axes.unicode_minus: False ``` 3. 删除缓存目录: ```bash rm -rf ~/.matplotlib/* ``` 4. 重启 Python 内核并重新运行代码。 ###
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值