Axes3D 数据标签和绘图限制
下面是一段小代码,说明了我在使用matplotlib的Axes3D实例时遇到的问题,主要是关于立方体的大小和坐标轴标签被截断的情况。虽然我可以很容易地改变图形画布的背景颜色,但这仍然会导致标签上的文字变得扭曲。有没有人知道如何最好地改变用于绘制数据的3D框的实际大小?单纯改变子图的尺寸似乎没有什么帮助。这个问题通过运行代码可以更清楚地看到。一旦绘制出来,日期格式的文字颜色会变化,一端会变得稍微暗一些。任何帮助都非常感谢。
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.dates as dates
import datetime, random
import matplotlib.ticker as ticker
def random_date():
date = datetime.date(2008, 12,01)
while 1:
date += datetime.timedelta(days=30)
yield (date)
def format_date(x, pos=None):
return dates.num2date(x).strftime('%Y-%m-%d') #use FuncFormatter to format dates
r_d = random_date()
some_dates = [dates.date2num(r_d.next()) for i in range(0,20)]
fig = plt.figure(facecolor = 'k')
ax = fig.add_subplot(111, projection='3d')
fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9)
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
xs = np.array(some_dates)
ys = np.random.rand(20)
ax.bar(xs, ys, zs=z, zdir='y', color=c, alpha=0.8,width=8)
ax.w_xaxis.set_major_locator(ticker.FixedLocator(some_dates)) # I want all the dates on my xaxis
ax.w_xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
for tl in ax.w_xaxis.get_ticklabels(): # re-create what autofmt_xdate but with w_xaxis
tl.set_ha('right')
tl.set_rotation(30)
ax.set_ylabel('Series')
ax.set_zlabel('Amount')
plt.show()
1 个回答
3
要更改距离 - ax.dist
- 可以这样做:
ax.dist = 15