0

I want to plot two data frames in one graph in 3D

data1 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [5,2,1,6,9,3,8,2,0,5]} 
data2 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [19,12,1,26,19,33,28,28,10,5]} 
newdf = pd.DataFrame(data1)
newdf2= pd.DataFrame(data2)
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(111, projection='3d')
newdf.plot(kind='bar',x ='numbers', y='frequency',figsize=(10,5), color='thistle', width=.4, legend=True, alpha=0.8, ax=ax)
newdf2.plot(kind='bar',x ='numbers', y='frequency',figsize=(10,5), color='navy', width=.2,legend=True, alpha=1,ax=ax) 
plot.show()

This plots both graphs in one graph, but the y and z axes are transposed. I want to plot each data set in the z-plane with the numbers forming the x-axis and the frequency the y-axis. I don't understand from all the examples how to achieve this. I would also like to plot the bars as 3d bars. I am grateful for any help, please

2 Answers 2

1

I'm just guessing because I don't have an example of the kind of output I'd like to see, but is the 3D graph you'd like to implement an example of the following: the y-axis is the type of data frame and the z-axis is the frequency.

import matplotlib.pyplot as plt

data1 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [5,2,1,6,9,3,8,2,0,5]} 
data2 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [19,12,1,26,19,33,28,28,10,5]} 
newdf = pd.DataFrame(data1)
newdf2 = pd.DataFrame(data2)

fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111, projection='3d')

yticks = [4,3,2,1,0]
ax.bar(newdf['numbers'], newdf['frequency'], zs=3, zdir='y', color='b', alpha=0.8)
ax.bar(newdf2['numbers'], newdf2['frequency'], zs=1, zdir='y', color='r', alpha=0.8)

ax.set_xlabel('number')
ax.set_ylabel('df_type')
ax.set_zlabel('frequency')

ax.set_yticks(yticks)

plt.show()

enter image description here

bar3d type

# ax.bar3d(xpos, ypos, zpos, dx, dy, dz)
ax.bar3d(newdf['numbers'], 3, 0, dx=1, dy=1, dz=newdf['frequency'], color='b', alpha=0.6)
ax.bar3d(newdf2['numbers'], 0, 0, dx=1, dy=1, dz=newdf2['frequency'], color='r', alpha=0.3)

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to make the bars 3D as well? Very clear solution. What does the 111 in the add_subplot(111,projection ='3d') pertain to?
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, zsort='average')Kindly refer to this.
Thank you and for the referral to the documentation. I understand how to use this now thanks to your clarity
0

Here is a solution that I have at hand (can't find the link to the original).

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

# datasets
data1 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [5,2,1,6,9,3,8,2,0,5]} 
data2 = {'numbers': [1,2,3,4,5,6,7,8,9,10], 'frequency': [19,12,1,26,19,33,28,28,10,5]} 

newdf = pd.DataFrame(data1)
newdf2= pd.DataFrame(data2)

# put all the data in one place
# can use data1['frequency'] and data2['frequency'] directly
data = np.array([
        newdf['frequency'].values,
        newdf2['frequency'].values,
        ])

fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')
colors = ["r","g","b"]*5  # for up to 15 sets of bars

# Draw 3D bars 
ncnt, nbins = data.shape[:2]
xs = np.arange(nbins)
for i in range(ncnt):
    ys = data[i]
    cs = [colors[i]] * nbins
    ax.bar(xs, ys.ravel(), zs=i, zdir='x', color=cs, alpha=0.8)

ax.set_xlabel('data_frame')
ax.set_ylabel('numbers')
ax.set_zlabel('frequency')

ax.set_xticks(range(data.shape[0]))    # 2 dataframes
ax.set_yticks(newdf['numbers'].values) # from 'numbers' column

plt.show()

The output plot:

output-plot

1 Comment

A very xclear solution. The reason I accepted the one below is simply because it was shorter. I would like to accept and vote up both solutions. Can the bars be plotted three-dimensional as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.