[英]Axis labels not spacing correctly
I am trying to plot column data vs the row label of a data frame.我正在尝试 plot 列数据与数据帧的 label 行。 When I do so, the plot looks good but the the Y axis starts to look illegible as the number of rows is increased.
当我这样做时,plot 看起来不错,但随着行数的增加,Y 轴开始看起来难以辨认。 What I don't get it why does the automatic spacing for the X axis work fine but not the same for the Y axis.
我不明白为什么 X 轴的自动间距工作正常但 Y 轴的自动间距不一样。
x1 = M.iloc[:,1]
plt.plot(x1,x)
Where the variable "x" represents Column 0 values of dataframe "M" below其中变量“x”代表下面 dataframe“M”的第 0 列值
The "M" dataframe: "M" dataframe:
0.0 0.5 1.0
0 300 300.000000 1550
1.00e-01 s 300 300.769527 1550
2.00e-01 s 300 301.538106 1550
3.00e-01 s 300 302.305739 1550
.
.
.
2.80e+00 s 300 321.192396 1550
2.90e+00 s 300 321.935830 1550
Edit编辑
So it seems it's the formatting of the first column being in scientific notation that is messing things up, still not sure why however所以似乎是第一列的格式是科学记数法把事情搞砸了,但仍然不知道为什么
x = [0]
i=1
while i < 30:
q = i*0.1
xx = str('{:.2e}'.format(q)) + ' s'
x.append(xx)
i = i + 1
M = pd.DataFrame(index=x, columns=3)
So in the code above, it is the line xx = str('{:.2e}'.format(q)) + ' s'
that is making the Y-labels go crazy.所以在上面的代码中,是
xx = str('{:.2e}'.format(q)) + ' s'
行让 Y 标签 go 变得疯狂。 I unfortunately can't take it out as I need them to be in scientific notation.不幸的是,我无法将其取出,因为我需要它们以科学记数法表示。
You can try tick-spacing if okay to eliminate few tick labels.如果可以消除一些刻度标签,您可以尝试刻度间距。 Other options are to increase you plot size or decrase font size for y labels.
其他选项是增加 plot 大小或减小 y 标签的字体大小。
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
x1 = M.iloc[:,1]
tick_spacing = 2 # or whatever label gap you want to use.
fig, ax = plt.subplots(1,1)
apx.plot(x1,x)
ax.yaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.