简体   繁体   English

matplotlib:如何 plot 3d 直方图

[英]matplotlib: How to plot 3d histogram

I have a dataframe that looks like following:我有一个 dataframe,如下所示:

      date         shop    success
1     12/06/2020   A       0.99
2     15/06/2020   A       0.95
3     17/07/2020   B       0.94
4     22/07/2020   B       0.97
...

I would like to plot a 3d histogram similar to this type:我想 plot 一个类似于这种类型的 3d 直方图:

示例 3d hist

The three dimensions would be:这三个维度将是:

  • x: date x:日期
  • y: shop y:商店
  • z: success z:成功

I went through many websites, but couldn´t figure out a way to do it.我浏览了许多网站,但无法找到一种方法。 I am pretty new with programming.我对编程很陌生。

Thanks for your help!!谢谢你的帮助!!

Short Answer: It can be done but you have to modify your DataFrame structure.简短回答:可以,但您必须修改 DataFrame 结构。

Long Answer:长答案:

Necessary libraries:必要的库:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

The columns "Date" and "Shop" in your Dataframe are not numerical value. Dataframe 中的“日期”和“商店”列不是数值。 This means, you have to construct the graph without them and then use them to label the ticks on the axes.这意味着,您必须在没有它们的情况下构建图形,然后将它们用于 label 轴上的刻度。 So lets firs constrict the empty graph.因此,让我们先压缩空图。 This is the dataframe structure you will need.这是您需要的 dataframe 结构。

df = pd.DataFrame({"A": [0.0, 0.0, 0.0, 0.0],"B": [0.0, 0.0, 0.0, 0.0]})

You need to group all the values by the "Shops" and use "Shops" your main variable!您需要按“商店”对所有值进行分组,并使用“商店”作为主要变量!

# Setting length and wight of the bars
dx, dy = .9, .01

# prepare 3d axes
fig = plt.figure(figsize=(6,6))
ax = Axes3D(fig)

# set bar positions on axes
xpos=np.arange(df.shape[0])
ypos=np.arange(df.shape[1])

# set the ticks in the middle of the bars
ax.set_xticks(xpos + dx/2)
ax.set_yticks(ypos + dy/2)

# create X, Y grid 
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten()
ypos = ypos.flatten()

# set the start of the bar to 0
zpos=np.zeros(df.shape).flatten()

# the bar heights these are the values under the column A and B
dz = df.values.ravel()

# ploting the barchart
ax.bar3d(xpos,ypos,zpos,dx,dy,dz)

# Labeling the ticks. For simplicity's sake I used lists for labeling but you can also iterate through columns "Date" and "Shop" to get the label values
ax.w_yaxis.set_ticklabels(["A", "B"])
ax.w_xaxis.set_ticklabels(["12/06/2020", "15/06/2020", "17/07/2020", "22/07/2020" ])

# Label the axes
ax.set_xlabel("Date")
ax.set_ylabel("Shop")
ax.set_zlabel("Success")

plt.show()

Output: Output:

在此处输入图像描述

Perfect!完美的! As you can see we have our base with shops A & B on one axis and dates on the other.如您所见,我们的基地在一个轴上设有商店 A 和 B,在另一轴上设有日期。 Now we can input some data into the original DataFrame and define "Success" Z values.现在我们可以在原始 DataFrame 中输入一些数据并定义“成功”Z 值。

df = pd.DataFrame({"a": [0.1, 0.2, 0.3, 0.4],"b": [0.2, 0.3, 0.4, 0.5]})

在此处输入图像描述

The more shops you have the more Axes you'll have...您拥有的商店越多,您拥有的斧头就越多……

df = pd.DataFrame({"a": [0.1, 0.2, 0.3, 0.4],"b": [0.2, 0.3, 0.4, 0.5], "c": [0.1, 0.4, 0, 1.4]})

在此处输入图像描述

You can style the bars with traditional arguments like "alpha=" and "color=" but keep in mind that you have several columns so you have to provide lists for each argument and not a single value.您可以使用传统的 arguments (如“alpha=”和“color=”)设置条形样式,但请记住,您有多个列,因此您必须为每个参数提供列表,而不是单个值。

Cheers:)干杯:)

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

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