Fill graph line in Python and change axis numbers to strings

Asked

Viewed 59 times

1

Good afternoon. This is the first time I’ve ever done a 3D graphic in Python, in Anaconda’s Jupyter Notebook. The goal is for the chart to look like this formatting: inserir a descrição da imagem aqui

My code is this::

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

fig = plt.figure()
ax = Axes3D(fig)


df = pd.read_excel('EDS 7.xlsx', header=None, usecols=[0,1,2,3,4,5,6,7,8,9,10,11,12], names=['A', 'B', 'C', 'D','E','F','G','H', 'I', 'J', 'K', 'L'])
plt.rcParams["figure.figsize"] = [14.5,10]
# put 0s on the y-axis, and put the y axis on the z-axis
ax.plot(xs=df['A'], ys=df['B'], zs=df['C'], zdir='z', label='ys=0, zdir=z', color='blue')
ax.plot(xs=df['D'], ys=df['E'], zs=df['F'], zdir='z', label='ys=0, zdir=z', color='red')
ax.plot(xs=df['G'], ys=df['H'], zs=df['I'], zdir='z', label='ys=0, zdir=z', color='green')
ax.plot(xs=df['J'], ys=df['K'], zs=df['L'], zdir='z', label='ys=0, zdir=z', color='orange')
plt.xlim([0.0, 4.0])
plt.ylim([0.0, 4.0])
ax.set_zlim(0,1200)
plt.rc('xtick', labelsize=16)
plt.rc('ytick', labelsize=16)
#plt.xticks(np.arange(0.0,1.4,0.1).round(decimals=1))
#plt.yticks(np.arange(-0.8,1.3,0.2).round(decimals=1))
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.xaxis._axinfo["grid"]['linestyle'] = '--'
ax.xaxis._axinfo["grid"]['color'] = 'silver'
ax.yaxis._axinfo["grid"]['linestyle'] = '--'
ax.yaxis._axinfo["grid"]['color'] = 'silver'
ax.zaxis._axinfo["grid"]['linestyle'] = '--'
ax.zaxis._axinfo["grid"]['color'] = 'silver'
ax.tick_params(axis='z', pad=10)
plt.show()

Upshot:

pyth

Excel file (not complete because otherwise the numbers will be too small, but serves as an example):

inserir a descrição da imagem aqui

I already got the grid and the lines and I changed the letter size of the axes, but I’m missing two things:

  • Fill the bottom of the lines with color;
  • Place strings on the right axis for the same chart orientation.

How can I do that?

1 answer

1


We have to define the ytick Labels and add the 3D shape polygons to fill.

labels = [item.get_text() for item in ax.get_yticklabels()]
labels[1] = 'Label1'
labels[2] = 'Label2'
labels[3] = 'Label3'
labels[4] = 'Label4'

ax.set_yticklabels(labels)

cols = [['A', 'B', 'C', 'blue'], ['D', 'E', 'F', 'red'], ['G', 'H', 'I', 'green'], ['J', 'K', 'L', 'orange']]
h = 0.0
for col in cols:
    v = []
    xs = df[col[0]]
    ys = df[col[1]]
    zs = df[col[2]]
    for k in range(0, len(xs) - 1):
        x = [xs[k], xs[k+1], xs[k+1], xs[k]]
        y = [ys[k], ys[k+1], ys[k+1], ys[k]]
        z = [zs[k], zs[k+1],       h,     h]
        v.append(list(zip(x, y, z))) 

    poly = Poly3DCollection(v)
    poly.set_facecolor(col[3])
    ax.add_collection3d(poly)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.