用matplotlib画图时,出现了这一堆东西,怎么让这一堆东西不要出现呀
这是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://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()
针对问题标题“如何让matplotlib画图时不显示多余信息?”,可以考虑以下几种方案:
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() # 显示图像
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() # 显示图像
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() # 显示图像
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() # 显示图像
需要根据具体情况选择合适的方案。