关于#python#的问题:用matplotlib画图时

用matplotlib画图时,出现了这一堆东西,怎么让这一堆东西不要出现呀

img

这是Matplotlib绘图库中XTick对象的输出结果,用于显示x轴上的刻度标签。如果您不希望看到这些输出信息,可以使用plt.xticks()函数来自定义x轴的刻度标签。

示例代码:

import matplotlib.pyplot as plt

x_values = [-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50]
y_values = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

plt.plot(x_values, y_values)
plt.xticks(x_values) # 使用x_values来设置x轴刻度标签
plt.show()

在这个示例中,plt.xticks()函数将x轴的刻度标签设置为x_values列表中的数值。这样就不会显示上述提示信息了,而是直接显示x轴上的数值。
你试试这样改是不是就好了

不出现这个那你坐标轴用什么

  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7591094
  • 这篇博客你也可以参考下:【Python-Matplotlib】 使用matplotlib模块显示彩色图像出现失真、颜色模式很奇怪的问题
  • 除此之外, 这篇博客: Python绘图表学习笔记 matplotlib绘图库中的 matplotlib如何绘制比例柱状图? 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 参考:https://www.zhihu.com/question/326181892/answer/695693441
    在这里插入图片描述

    
    import numpy as np
    import matplotlib.pyplot as plt
    
    people = ('1','2','3','4','5','6','7','8')
    segments = 4
    
    # multi-dimensional data
    data = np.asarray([[  3.40022085,   7.70632498,   6.4097905,   10.51648577,   7.5330039,
        7.1123587,   12.77792868,   3.44773477],
     [ 11.24811149,   5.03778215,   6.65808464,  12.32220677,   7.45964195,
        6.79685302,   7.24578743,   3.69371847],
     [  3.94253354,   4.74763549,  11.73529246,   4.6465543,   12.9952182,
        4.63832778,  11.16849999,   8.56883433],
     [  4.24409799,  12.71746612,  11.3772169,    9.00514257,  10.47084185,
       10.97567589,   3.98287652,   8.80552122]])
    
    percentages = np.zeros((8, 4))
    col_sum = np.sum(data, axis=0)
    for i in range(data.shape[0]):
        for j in range(len(data[i])):
            percentages[j, i] = data[i, j] / col_sum[j] * 100
    
    y_pos = np.arange(len(people))
    
    fig = plt.figure(figsize=(10,8))
    ax = fig.add_subplot(111)
    
    colors ='rgbm'
    patch_handles = []
    
    bottom = np.zeros(len(people))
    for i, d in enumerate(data):
        patch_handles.append(ax.bar(y_pos, d,
          color=colors[i%len(colors)], align='center',
          bottom=bottom))
        bottom += d
    
    # search all of the bar segments and annotate
    for j in range(len(patch_handles)):
        for i, patch in enumerate(patch_handles[j].get_children()):
            bl = patch.get_xy()
            x = 0.5*patch.get_width() + bl[0]
            y = 0.5*patch.get_height() + bl[1]
            ax.text(x,y, "%d%%" % (percentages[i,j]), ha='center')
    
    plt.show()
    
  • 您还可以看一下 覃秉丰老师的1天搞定Python进阶课程-Matplotlib绘图课程中的 matplotlib基础用法小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    针对问题标题“如何让matplotlib画图时不显示多余信息?”,可以考虑以下几种方案:

    1. 设置图像大小 可以通过plt.figure()函数设置图像大小,将图像大小调整为合适的大小,使得图像不会出现多余信息。示例代码如下:
    import matplotlib.pyplot as plt
    
    fig = plt.figure(figsize=(8, 6))  # 设置图像大小为8x6
    ax = fig.add_subplot(111)  # 添加一个子图
    ax.plot([1, 2, 3, 4], [1, 4, 2, 3])  # 绘制图像
    plt.show()  # 显示图像
    
    1. 去除坐标轴标签 可以通过设置坐标轴标签为空字符来去除坐标轴标签。示例代码如下:
    import matplotlib.pyplot as plt
    
    fig = plt.figure()  # 新建一个图像
    ax = fig.add_subplot(111)  # 添加一个子图
    # 绘制图像
    ax.plot([1, 2, 3, 4], [3, 1, 4, 2])
    # 去除x、y坐标轴标签
    ax.set_xlabel('')
    ax.set_ylabel('')
    plt.show()  # 显示图像
    
    1. 去除坐标轴刻度 可以通过设置坐标轴刻度为空列表来去除坐标轴刻度。示例代码如下:
    import matplotlib.pyplot as plt
    
    fig = plt.figure()  # 新建一个图像
    ax = fig.add_subplot(111)  # 添加一个子图
    # 绘制图像
    ax.plot([1, 2, 3, 4], [3, 1, 4, 2])
    # 去除x、y坐标轴刻度
    ax.set_xticks([])
    ax.set_yticks([])
    plt.show()  # 显示图像
    
    1. 设置轴界限 可以通过设置坐标轴界限来调整图像的显示范围,使得多余信息不显示出来。示例代码如下:
    import matplotlib.pyplot as plt
    
    fig = plt.figure()  # 新建一个图像
    ax = fig.add_subplot(111)  # 添加一个子图
    # 绘制图像
    ax.plot([1, 2, 3, 4], [3, 1, 4, 2])
    # 设置x、y轴界限
    ax.set_xlim(left=0.5, right=4.5)
    ax.set_ylim(bottom=0.5, top=4.5)
    plt.show()  # 显示图像
    

    需要根据具体情况选择合适的方案。