Python bar3d-如何对齐刻度标签和刻度

时间:2020-05-11 14:04:49

标签: python-3.x matplotlib 3d

我对bar3d有很大的疑问,找不到解决方法。下面是我从一些法语的分析中获得的图-如您所见-数据): enter image description here 问题:如何使x轴上的刻度标签与刻度对齐?在目前的情节上,它们已移动且位置不正确...

注意:为了在y轴上具有13个足够间隔的位置,我必须使用轴的比例尺(如我们在所附程序中看到的,使用ax.get_proj )。但是我敢肯定它不是从那里来的:我尝试了各种比例尺,但不良的定位仍然保持不变。

程序:如果您希望重现绘图...:

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
from matplotlib import cm
%matplotlib inline

# Data

cs = pd.DataFrame(
         [
             ["Auvergne Rhône-Alpes", 10, 62, 63, 31],
             ["Bourgogne-Franche-Comté", 3, 4, 3, 1],
             ["Bretagne", 9, 5, 6, 2],
             ["Centre-Val de Loire", 4, 4, 1, 1],
             ["Corse", 0, 0, 0, 1],
             ["Grand Est", 3, 7, 13, 3],
             ["Hauts-de-France", 33, 22, 41, 20],
             ["Normandie", 4, 3, 4, 0],
             ["Nouvelle-Aquitaine", 13, 11, 12, 5],
             ["Occitanie", 8, 56, 95, 122],
             ["Pays de la Loire", 3, 6, 5, 4],
             ["Provence-Alpes-Côte d'Azur", 0, 20, 32, 50],
             ["Île-de-France", 72, 119, 150, 60]
         ],
         columns = [
             "Région",
             "Jamais",
             "Moins d’une semaine",
             "De 1 à 4 semaines",
             "Plus de 4 semaines"
         ]
    )

# Setting the plot

regions = cs['Région'].tolist()
weeks = list(cs.columns.values)[1:5]

fig = plt.figure(figsize=(16,12))
ax = Axes3D(fig)

ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([0.5, 1.5, 0.75, 1]))

x_shape = cs.shape[1]-1
y_shape = cs.shape[0]
xy_shape = (x_shape, y_shape)

_x = np.arange(x_shape)
_y = np.arange(y_shape)
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.flatten(), _yy.flatten()

top = np.array(cs.select_dtypes(include=[np.number]).values.tolist()).flatten()
n = len(top)

bottom = np.zeros(xy_shape).flatten()
width = np.repeat(a=0.1, repeats=n)
depth = np.repeat(a=0.1, repeats=n)

values = np.linspace(0.0, 1.0, n)
colors = cm.rainbow(values)

ax.bar3d(x, y, bottom, width, depth, top, color = colors, alpha=0.5)

ax.set_xticks(_x)
ax.w_xaxis.set_ticklabels(weeks, rotation=35)

ax.tick_params(axis='both', labelsize=20)

#ax.set_ylabel('\n\n\nRégion', size=24)
#ax.set_xlabel('\n\n\n\nPrésence', size=24)
#ax.set_zlabel('\n\nNombre de cas', size=24)

plt.title("Présence en bord de Méditerranée\n", fontsize=24)

plt.show()

0 个答案:

没有答案
相关问题