pandas 数据框绘图栏下降值 [英] Pandas dataframe plot bar drops values

查看:63
本文介绍了 pandas 数据框绘图栏下降值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行数据分析时,我使用pandas Dataframe的df.plot.bar方法来绘制结果:

  df。 plot.bar(figsize =(15,5),legend = None)

同时进行更多研究我注意到,在某些情况下情节看起来有所不同。原因是,当figsize太小时,plot.bar方法开始忽略信息。



我的示例中每个事件有31个小节,而 figsize =(10,5)的小节数比 figsize(15,5)
这是已知行为和期望行为吗?因为在没有警告的情况下,情节错过了(重要的)信息。



数据帧:

 日(绘图中的X轴)
+ ------------ + --------------------- -+
| ID +第1天|第2天|第3天...
+ ------------ + ----------------------- +
| 0 + 0 | 20 | 0
| 1 | 300 | 10 | 400
| 3 + 20 | 0 | 400
| 4 | 60 | 0 | 800
...
+ ------------ + ----------------------- +

值是特定日期在特定日期的人口。



每个id每天都有自己的酒吧。数据帧用T换位以制作plot.bar在X轴上绘制日期。



figsize =(10,5)



解决方案

作为估计:您有12行31列。酒吧占据了单位面积的80%。该条至少应有两个像素宽才能在屏幕上看到。通常的数字在轴的两侧都有10%的边距,并且具有100 dpi。然后您需要2 * 12 * 31 / 0.8 ** 2/100〜= 12英寸的图形宽度。换句话说,如果您有超过31列,则在12英寸宽的图形上可能看不到一些条形。



为使此图具有可重复性,请考虑以下内容我们有 N = 20 行和列的情况。

  import matplotlib.pyplot as plt 
进口熊猫as pd
import numpy as np

N = 20
df = pd.DataFrame(np.diag(np.ones(N)))

ax = df.plot.bar(legend = False)

plt.show()



此处一些栏不可见。我们可以计算出,对于20行和20列,数字将需要为 2 * N ** 2 / 0.8 ** 2/100。 == 12.5 英寸英寸。

  N = 20 
df = pd.DataFrame (np.diag(np.ones(N)))

w = 2 * N ** 2 / 0.8 ** 2/100。
plt.rcParams.update({ figure.figsize:(w,4.8), figure.dpi:100})
ax = df.plot.bar(legend = False)

现在它确实显示了所有条形。



< a href = https://i.stack.imgur.com/6QTri.png rel = nofollow noreferrer>



当然,不能任意增大图形的大小,因此可以确保条形图具有边线,这样可以

  import matplotlib.pyplot as plt 
进口熊猫as pd
import numpy as np

N = 20
df = pd.DataFrame(np.diag(np.ones(N)))

ax = df .plot.bar(legend = False)
用于ax.patches中的bar:
bar.set_linewidth(0.72)
bar.set_edgecolor(bar.get_facecolor())
plt。 show()


While doing data analysis I uses the df.plot.bar method of pandas Dataframe to plot my results:

df.plot.bar(figsize=(15, 5), legend=None)

While doing some more research I noticed, that the plots look different in some cases. The reason for this is, that the plot.bar method starts to ignore information, when the figsize is too low.

My example had 31 bars per event and at figsize=(10, 5) less bars where plotted than at figsize(15, 5). Is this known and desired behaviour? Because without a warning the plot misses (important) information.

Dataframe:

                 Day(X-Axis in plot)
+------------+-----------------------+
| Id    +  Day1  |   Day2  |   Day3  ...
+------------+-----------------------+
| 0     + 0      |   20    |    0
| 1     | 300    |   10    |    400
| 3     + 20     |   0     |    400
| 4     | 60     |   0     |    800
...
+------------+-----------------------+

The values are the population of the specific id at a specific day.

Every id gets its own bar for every day. The dataframe is transposed with T in order to make plot.bar plot the days on the X axis.

figsize=(10, 5)

figsize(15, 5)

解决方案

As an estimate: You have 12 rows and 31 columns. Bars take 80% of the space of a unit. The bar should at least be two pixels wide to be seen on screen. A usual figure has 10% margins on both side of the axes and has 100 dpi. Then you need a figure width of 2*12*31/0.8**2/100 ~= 12 inch. In other words, if you you have more than 31 columns, some bars might not be visible on a 12 inch wide figure.

To make this reproducible, let's consider the following case where we have N=20 rows and columns.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

N=20
df = pd.DataFrame(np.diag(np.ones(N)))

ax = df.plot.bar(legend=False)

plt.show()

Here some bars are not visible. We can calculate that for 20 rows and 20 columns the figure would need to be 2*N**2/0.8**2/100. == 12.5 inch in width.

N=20
df = pd.DataFrame(np.diag(np.ones(N)))

w = 2*N**2/0.8**2/100.
plt.rcParams.update({"figure.figsize" : (w, 4.8), "figure.dpi" : 100})
ax = df.plot.bar(legend=False)

Now it indeed shows all bars.

Of course a figure cannot be made arbitrarily large, so one might instead make sure the bars have an edgeline, which would be drawn independent of the extent of the rectangle.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

N=20
df = pd.DataFrame(np.diag(np.ones(N)))

ax = df.plot.bar(legend=False)
for bar in ax.patches:
    bar.set_linewidth(0.72)
    bar.set_edgecolor(bar.get_facecolor())
plt.show()

这篇关于 pandas 数据框绘图栏下降值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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