我对这里的xticks图有一些问题:

有人能帮上忙吗?
我尝试了他们在这里所做的:Date ticks and rotation in matplotlib,但是没有用。
import numpy as np
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt
import datetime as DT
import matplotlib.dates as mdates
import matplotlib.ticker as ticker
#import data
i, time, temp, hum, light_lv, light_v = np.loadtxt('DHT11.csv', delimiter = ',', skiprows = 1,
usecols = (0,2,3,4,5,6), unpack = 1)
#id, unixtime, temp, humidity, lightlevel, lightvolt
time = [DT.datetime.fromtimestamp(t) for t in time]
light_lv = 250 - light_lv
xfmt = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')
if 1:
host = host_subplot(111, axes_class=AA.Axes)
host.xaxis.set_major_formatter(xfmt)
plt.subplots_adjust(right=0.75)
par1 = host.twinx()
par2 = host.twinx()
offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
axes=par2,
offset=(offset, 0))
par2.axis["right"].toggle(all=True)
#host.set_xlim(0, 25)
host.set_ylim(15, 25)
host.set_xlabel("Time (unix)")
host.set_ylabel("Temperature (C)")
par1.set_ylabel("Humidity (%)")
par2.set_ylabel("Light (A.U.)")
p1, = host.plot(time, temp)
p2, = par1.plot(time, hum)
p3, = par2.plot(time, light_lv)
#par1.set_ylim(0, 4)
#par2.set_ylim(1, 65)
host.legend()
host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())
plt.draw()
plt.show()
#plt.savefig("Test")发布于 2013-12-08 23:03:16
我发现在matplotlib中解决这类问题的最简单的方法是查看gallery,直到你找到一个你想要的东西,然后复制它。
Look at this one
你想要这个:
plt.xticks(x, labels, rotation='vertical')还有另一种使用here的技术
# Set the axes ranges and axes labels
ax1.set_xlim(0.5, numBoxes+0.5)
top = 40
bottom = -5
ax1.set_ylim(bottom, top)
xtickNames = plt.setp(ax1, xticklabels=np.repeat(randomDists, 2))
plt.setp(xtickNames, rotation=45, fontsize=8)或者,这看起来更简单,你可以只做这个答案中做的事情。
plt.xticks(rotation=<whatever>)在你计划任何事情之前先把它放进去。
发布于 2016-04-26 03:11:43
我和你有完全一样的问题。我添加了以下行,并能够旋转自定义x轴记号标签:
ax1.axis["bottom"].major_ticklabels.set_axis_direction("left")我在文档的这一页上找到了这一小段代码-请参阅“TickLabels的旋转和对齐”部分:http://matplotlib.org/mpl_toolkits/axes_grid/users/axisartist.html
我还偶然发现了一个devdocs页面,上面写着大多数典型的与tick相关的mpl代码并不能真正与mpl_toolkits.axisartist实现一起工作。
在AxisGrid文档的顶部有一个类似的警告:http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html?highlight=host_subplot
https://stackoverflow.com/questions/20454759
复制相似问题