Matplotlib条形图-一些条形图不可见,宽度似乎不同 [英] Matplotlib bar chart- some bars are not visible and seem to be of different width

查看:30
本文介绍了Matplotlib条形图-一些条形图不可见,宽度似乎不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题与matplotlib条形图有关.具体来说,当我绘制条形图时,某些条形不可见.

The problem is connected with the matplotlib bar chart. To be specific, when I draw a bar chart some of the bars are not visible.

点和条使用相同的数据系列,所以应该在相同的地方.

Dots and bars uses the same data series so should be in the same places .

我不知道此问题的根源,这就是为什么我发布此问题.我还注意到,对于同一个情节,当我将窗口放大到全屏时,丢失的条会神奇地出现

I do not know what is the source of this problem that is why I am posting this question. I have also noticed that for the same plot, when I enlarge the window to the full screen the missing bars magically appears

与第一张图片相同的数据系列,只是全屏窗口.

The same data series as in the first picture, just full screen window .

当我改变情节的颜色时也会发生同样的情况,所以假设我使用:

The same happens also when I change the color of the plot, so lets say I use:

artist = plot.bar(x_data_series, y_data_series, color="black")

现在我有这个不完整的图表,但是当我使用时(只是在之后第二次设置颜色):

Right now I have this not complete chart, but when I use (just setting the color for the second time right after):

artist = plot.bar(x_data_series, y_data_series, color="black")
matplotlib.artist.setp(artist, color="black")

我明白了:

现在所有条形都可见.

当我放大此不完整的图表时,也会发生同样的情况:

The same happens when I zoom in this incomplete chart:

这里也可以看到所有条形.

Here also all bars are visible .

更重要的是,不可见条"在可见时似乎比其他的要粗一些,但是它们在艺术家属性中的宽度与您在此处看到的宽度相同:

What is more the "invisible bars", when they are visible, they seem to be a little bit thicker than the others, however they width in the artist properties is the same as you can see here:

其中一个是较粗的,另一个是正常"的,宽度均为 0.8.

One of this bars is the thicker one and the other the "normal" one, both are 0.8 in width .

只是为了给您更多信息,这是嵌入在 tkinter 中的图表.这是图形,画布和轴(父框架只是放置绘图的框架):

Just to give you more information this is a graph embedded in the tkinter. Here are the figure, canvas, and axes (parent frame is just a frame in which the plot is placed):

figure = Figure(dpi=100)
plot = figure.add_subplot(111)
canvas = FigureCanvasTkAgg(figure, parent_frame)

我发现的变通办法"对我来说并不令人满意,我真的很想找到问题的根源,再加上宽度问题是我不知道的.

The "work arounds" I have found are not satisfactory for me and I would really like to find the source of the problem, plus the width problem is something I can't figure out.

推荐答案

条宽 0.8 表示条宽为 0.8 个数据单位.在约 500 像素宽的图形上显示约 500 个数据单元意味着条形为约 0.8 像素宽.或者换句话说,看到条形的机会为 80% 或平均而言,可能不会显示每五个条形.所以结果还是比较期待的.

A bar width of 0.8 means that bars are 0.8 data units wide. Showing ~500 data units on the graph which is ~500 pixels wide means a bar is ~0.8 pixels wide. Or in other words, chances to see the bar are 80% or on average, every fifth bar may not be shown. So the result is rather expected.

放大时也是如此.看到条形覆盖 1 个像素的可能性很高,但有些条形的宽度为 2 个像素,因为条形宽度介于 1 到 2 个像素之间.

Same when you zoom in. Chances are high to see the bar cover one pixel, but some bars are 2 pixels wide, because the bar width is somewhere in between 1 and 2 pixels.

使用 setp(artist,color ="black")时看到每个条的原因是该颜色适用于条及其边缘.边缘为1点,即比像素大一点,因此每个条都可见.

The reason you see every bar when using setp(artist, color="black") is that the color applies to the bar and its edge. The edge is 1 point, i.e. a little bigger than a pixel, such that each bar is visible.

最后,您似乎想要条形图.在这种情况下,使条形成为具有某种有意义的数据单位宽度的真实条形.或者,您可能需要折线图.IE.在这里绘制 stem 图可能是有意义的.

At the end it seems you either want a bar graph. In that case, make the bar become a real bar with some meaningful width in data units. Or, you may want a line graph. I.e. plotting a stem plot may make sense here.

这篇关于Matplotlib条形图-一些条形图不可见,宽度似乎不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆