回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>尝试在matplotlib中旋转日期刻度时遇到问题。下面是一个小的示例程序。如果我尝试旋转末端的记号,则记号不会旋转。如果我尝试按注释“crashes”下所示旋转记号,则matplot lib将崩溃。</p>
<p>只有当x值是日期时才会发生这种情况。如果我在对<code>avail_plot</code>的调用中将变量<code>t</code>替换为变量<code>dates</code>,则<code>xticks(rotation=70)</code>调用在<code>avail_plot</code>内部工作正常。</p>
<p>有什么想法吗?</p>
<pre><code>import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
def avail_plot(ax, x, y, label, lcolor):
ax.plot(x,y,'b')
ax.set_ylabel(label, rotation='horizontal', color=lcolor)
ax.get_yaxis().set_ticks([])
#crashes
#plt.xticks(rotation=70)
ax2 = ax.twinx()
ax2.plot(x, [1 for a in y], 'b')
ax2.get_yaxis().set_ticks([])
ax2.set_ylabel('testing')
f, axs = plt.subplots(2, sharex=True, sharey=True)
t = np.arange(0.01, 5, 1)
s1 = np.exp(t)
start = dt.datetime.now()
dates=[]
for val in t:
next_val = start + dt.timedelta(0,val)
dates.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(next_val)
start = next_val
avail_plot(axs[0], dates, s1, 'testing', 'green')
avail_plot(axs[1], dates, s1, 'testing2', 'red')
plt.subplots_adjust(hspace=0, bottom=0.3)
plt.yticks([0.5,],("",""))
#doesn't crash, but does not rotate the xticks
#plt.xticks(rotation=70)
plt.show()
</code></pre>