python在xaxis上旋转值不重叠 [英] python rotate values on xaxis to not overlap

查看:270
本文介绍了python在xaxis上旋转值不重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



任何人都可以帮忙吗?

我尝试了他们在这里所做的:日期ticks和旋转matplotlib 但无济于事。

  import numpy as np 
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
导入matplotlib.pyplot为plt
导入日期时间为DT
导入matplotlib.dates作为mdates
导入matplotlib.ticker作为ticker

#导入数据
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时间]
light_lv = 250 - light_lv

xfmt = mdates.DateFormatter('%Y-%m-%d%H:%M:%S')

如果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 (湿度(%))
par2.set_ylabel(Light(AU))

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(T我找到了解决这个问题的最简单的方法。我找到了解决这个问题的最简单的方法。像matplotlib这样的东西只是通过 gallery 浏览,直到找到一个你想要的东西,然后复制该位。



看看这个



您需要:

  plt.xticks (x,labels,rotation ='vertical')

还有另外一个技巧这里

 #设置轴范围和轴标签
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,fontsi ze = 8)

或许,这看起来更简单,你可以只做这个答案。

  plt.xticks(rotation =< whatever>)

只需在绘制任何东西之前放入即可。


I'm having some problems with the xticks of the graph here:

Can anyone help?

I tried what they did here: Date ticks and rotation in matplotlib but to no avail.

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")

解决方案

I find the easiest way to work out things like this in matplotlib is just look through the gallery until you find one that has what you want, and just copy that bit.

Look at this one

You want this:

plt.xticks(x, labels, rotation='vertical')

There is another techniqued used 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)

alternatively, and this looks easier, you can just do what's done in this answer.

plt.xticks(rotation=<whatever>)

just put that in before you plot anything.

这篇关于python在xaxis上旋转值不重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆