简体   繁体   English

x 轴 matplotlib 中的意外负值

[英]Unexpected negative values in x-axis matplotlib

I have the following x and y values:我有以下 x 和 y 值:

x = [0.  , 0.02, 0.04, 0.06, 0.08, 0.1 , 0.12, 0.14, 0.16, 0.18, 0.2 ,
       0.22, 0.24, 0.26, 0.28, 0.3 , 0.32, 0.34, 0.36, 0.38, 0.4 , 0.42,
       0.44, 0.46, 0.48, 0.5 , 0.52, 0.54, 0.56, 0.58, 0.6 , 0.62, 0.64,
       0.66, 0.68, 0.7 , 0.72, 0.74, 0.76, 0.78, 0.8 , 0.82, 0.84, 0.86,
       0.88, 0.9 , 0.92, 0.94, 0.96, 0.98]

y = [4179,  628,   41,  142,  117,    6,   11,    2,    1,    0,    6,
          2,   12,    5,    7,    3,    2,    3,    2,    3,   30,   27,
         31,   29,   14,   31,   22,   28,   18,   19,   19,   22,   20,
         19,   21,   23,   25,   24,   20,   29,   23,   25,   31,   25,
         24,   28,   23,   26,   32,   21]

I'm plotting a bar chart using the code我正在使用代码绘制条形图

fig, cx = plt.subplots(figsize =(15, 7))
cx.bar(x,y)
plt.show()

Which is giving me the following plot这给了我以下情节在此处输入图片说明

This plot is unexpected because as shown in the diagram, the values of the x-axis lie between 0 and 1. There are no negative values and no values greater than 1 in the data.此图出乎意料,因为如图所示,x 轴的值介于 0 和 1 之间。数据中没有负值,也没有大于 1 的值。 I wasn't sure about the reason for the presence of bars beyond the 0-1 region.我不确定在 0-1 区域之外出现条形的原因。

I tried out the solutions given in the following two question links: Why is Python Matplotlib bar-chart's X axis ticks showing strange and wrong negative values?我尝试了以下两个问题链接中给出的解决方案: 为什么 Python Matplotlib 条形图的 X 轴刻度显示奇怪和错误的负值? and Matplotlib yaxis range display using absolute values rather than offset values?Matplotlib yaxis 范围显示使用绝对值而不是偏移值?

I tried using the following solutions:我尝试使用以下解决方案:

  1. cx = plt.gca() cx.ticklabel_format(useOffset=False)
  2. plt.bar(x, y, tick_label=x)
  3. cx.get_xaxis().get_major_formatter().set_useOffset(False)
  4. cx = plt.gca() cx.set_xticklabels(ax.get_xticks())

Solutions #1, #3, and #4 didn't lead to any changes.解决方案#1、#3 和#4 没有导致任何变化。 Solution #2 added ticks and the values were overlapped as shown in the figure below.解决方案#2 添加了刻度,并且值重叠,如下图所示。 But the bars didn't change.但酒吧没有改变。

在此处输入图片说明

Since few of the bars in the middle regions of the graphs have smaller bar widths it doesn't seem like bigger bar width is the issue here.由于图形中间区域中的条形很少有较小的条形宽度,因此这里的问题似乎不是较大的条形宽度。

Any idea to solve this is appreciated.任何解决这个问题的想法都值得赞赏。 Thanks in advance :)提前致谢 :)

The documentation for plt.bar() should clear things up. plt.bar()的文档应该清除问题。

The function creates bars centered at x , each with width width (0.8 by default).该函数创建x为中心的条形,每个条形的宽度为width (默认为 0.8)。 So your first bar is centered at x = 0 and spans from -0.4 to 0.4.所以你的第一个条以 x = 0 为中心,范围从 -0.4 到 0.4。 Likewise for the last one.最后一个也一样。 You can change the alignment or width (smaller would make them thinner), but that's about it.您可以更改对齐方式或宽度(更小会使它们更薄),但仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM