本文介绍了如何更改 Python 图中的 x 轴标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

import numpy as np
import pylab as plt

a = np.array([1,2,3,4,5,6,7,8,9,10])
b = np.exp(a)
plt.plot(a,b,'.')
plt.show()

代码工作正常,但我需要修改绘图的 x 轴标签.我希望根据 a 轴输入,x轴标签全部为10的幂.对于示例代码,它类似于 [10^1, 10^2, ..., 10^10].

The code works fine, but I need to modify the x-axis labels of the plot.I would like the x-axis labels to be all powers of 10 according to the a axis inputs. for the example code, it would be like [10^1, 10^2, ..., 10^10].

任何建议,我将不胜感激.

I would appreciate any suggestions.

谢谢!

推荐答案

这段代码可能就是你所需要的:

This code probably is what you need:

import numpy as np
import pylab as plt

a = np.asarray([1,2,3,4,5,6,7,8,9,10])
b = np.exp(a)
c = np.asarray([10**i for i in a])
print(list(zip(a,c)))
plt.xticks(a, c)
plt.plot(a,b,'.')
plt.show()

通过使用 plt.xtick(),您可以自定义绘图的 x 标签.我还用 10 ** i 替换了 10 ^ i .

By using plt.xtick() you can customize your x-label of plot. I also replaced 10^i with 10**i.

这篇关于如何更改 Python 图中的 x 轴标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:57
查看更多