在pycharm中使用plt.tight_layout()之后显示不出图形的问题

在PyCharm中遇到使用plt.tight_layout()后无法显示图形的问题,可以通过添加plt.show()来解决。这行代码能让图形在PyCharm的右侧窗口正确显示出来,确保Python数据可视化过程的正常进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在pycharm中使用plt.tight_layout()之后显示不出图形的问题,解决问题的方法就是在下面添加plt.show()这行代码,就会在pycharm界面的右侧显示图形。

如何解决D:\python\python3.9.5\python.exe C:/Users/马斌/Desktop/NGSIM_data_processing/30s/x-y.py C:\Users\马斌\Desktop\NGSIM_data_processing\30s\x-y.py:12: MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later. plt.plot(x, y) C:\Users\马斌\Desktop\NGSIM_data_processing\30s\x-y.py:20: MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later. plt.show() D:\pyCharm\PyCharm 2020.2\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 36724 (\N{CJK UNIFIED IDEOGRAPH-8F74}) missing from current font. self.figure.tight_layout() D:\pyCharm\PyCharm 2020.2\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 25968 (\N{CJK UNIFIED IDEOGRAPH-6570}) missing from current font. self.figure.tight_layout() D:\pyCharm\PyCharm 2020.2\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 25454 (\N{CJK UNIFIED IDEOGRAPH-636E}) missing from current font. self.figure.tight_layout() D:\pyCharm\PyCharm 2020.2\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font. self.figure.tight_layout() D:\pyCharm\PyCharm 2020.2\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: Glyph 26512 (\N{CJK UNIFIED IDEOGRAPH-6790}) missing from current font. self.figure.tight_layout() D:\pyCharm\PyCharm 2020.2\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:69: UserWarning: Glyph 36724 (\N{CJK UNIFIED IDEOGRAPH-8F74}) missing from current font. FigureCanvasAgg.draw(self) D:\pyCharm\PyCharm 2020.2\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:69: UserWarning: Glyph 25968 (\N{CJK UNIFIED IDEOGRAPH-6570}) missing from current font. FigureCanvasAgg.draw(self) D:\pyCharm\PyCharm 2020.2\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:69: UserWarning: Glyph 25454 (\N{CJK UNIFIED IDEOGRAPH-636E}) missing from current font. FigureCanvasAgg.draw(self) D:\pyCharm\PyCharm 2020.2\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:69: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font. FigureCanvasAgg.draw(self) D:\pyCharm\PyCharm 2020.2\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:69: UserWarning: Glyph 26512 (\N{CJK UNIFIED IDEOGRAPH-6790}) missing from current font. FigureCanvasAgg.draw(self)
05-25
``` import pandas as pd import matplotlib.pyplot as plt # 设置图片清晰度 plt.rcParams['figure.dpi'] = 300 # 设置中文字体 plt.rcParams['font.sans-serif'] = ['WenQuanYi Zen Hei'] # 从 CSV 文件中读取数据 file_path = r'D:\Downloads\600000.SH.csv' # 请替换为实际的 CSV 文件路径 import chardet # 检测文件编码 with open(r'D:\Downloads\600000.SH.csv', 'rb') as f: rawdata = f.read() result = chardet.detect(rawdata) encoding = result['encoding'] # 使用检测到的编码读取文件 df = pd.read_csv(r'D:\Downloads\600000.SH.csv', encoding=encoding) # 将日期列转换为日期时间类型 df['日期'] = pd.to_datetime(df['日期']) # 设置日期列为索引 df.set_index('日期', inplace=True) # 定义要绘制的列名 columns_to_plot = ['前收盘价(元)', '开盘价(元)', '最高价(元)', '最低价(元)'] # 循环绘制每个列的折线图 for column in columns_to_plot: plt.figure(figsize=(12, 6)) df[column].plot() plt.title(f'{column} 走势') plt.xlabel('日期') plt.ylabel('价格(元)') plt.show()```findfont: Generic family 'sans-serif' not found because none of the following families were found: WenQuanYi Zen Hei Traceback (most recent call last): File "C:\Users\杨阳\PycharmProjects\pythonProject\test.py", line 39, in <module> plt.show() File "C:\Users\杨阳\PycharmProjects\pythonProject\.venv\Lib\site-packages\matplotlib\pyplot.py", line 614, in show return _get_backend_mod().show(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\PyCharm 2024.3.4\PYCHARM\PyCharm 2024.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 41, in __call__ manager.show(**kwargs) File "D:\PyCharm 2024.3.4\PYCHARM\PyCharm 2024.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 144, in show self.canvas.show() File "D:\PyCharm 2024.3.4\PYCHARM\PyCharm 2024.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 85, in show buffer = self.tostring_rgb() ^^^^^^^^^^^^^^^^^ AttributeError: 'FigureCanvasInterAgg' object has no attribute 'tostring_rgb'. Did you mean: 'tostring_argb'?
最新发布
03-14
下面这份代码修改成可以在pycharm显示出来的 data['persqm'] = pd.to_numeric(data['persqm'], errors='coerce') data = data.dropna(subset=['persqm']) price_level = pd.cut(data['persqm'], bins=[0, 10000, 20000, 30000, 40000, float('inf')], labels=['0-1万', '1-2万', '2-3万', '3-4万', '4万以上']) area_level = pd.cut(data['square'], bins=[0, 10, 20, 30, 40, 50, 60, 70, float('inf')], labels=['0-10', '10-20', '20-30', '30-40', '40-50', '50-60', '60-70', '70以上']) house_type = data['house_type'] direction = data['direction'] deco = data['deco'] fig, axs = plt.subplots(2, 2, figsize=(12, 8)) fig.suptitle('房价与特征之间的关系', fontsize=16) # Subplot 1: House type vs Price level axs[0, 0].scatter(house_type, price_level, alpha=0.6) axs[0, 0].set_xlabel('房型', fontsize=12) axs[0, 0].set_ylabel('每平米房价(万元)', fontsize=12) # Subplot 2: Area level vs Price level axs[0, 1].scatter(area_level, price_level, alpha=0.6) axs[0, 1].set_xlabel('房屋面积(平方米)', fontsize=12) axs[0, 1].set_ylabel('每平米房价(万元)', fontsize=12) # Subplot 3: Direction vs Price level axs[1, 0].scatter(direction, price_level, alpha=0.6) axs[1, 0].set_xlabel('朝向', fontsize=12) axs[1, 0].set_ylabel('每平米房价(万元)', fontsize=12) # Subplot 4: Decoration vs Price level axs[1, 1].scatter(deco, price_level, alpha=0.6) axs[1, 1].set_xlabel('装修情况', fontsize=12) axs[1, 1].set_ylabel('每平米房价(万)', fontsize=12) axs[1, 1].grid(True, linestyle='--', alpha=0.4) plt.rcParams['axes.unicode_minus'] = False plt.tight_layout() plt.show()
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老师你好ss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值