gpt4 book ai didi

python在x轴上旋转值以不重叠

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:25 25 4
gpt4 key购买 nike

我在这里遇到图表的 xticks 问题: enter image description here

有人能帮忙吗?

我试过他们在这里做了什么: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")

最佳答案

我发现在 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>)

在你绘制任何东西之前把它放进去。

关于python在x轴上旋转值以不重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20454759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com