2

I'm having some problems with the xticks of the graph here: enter image description 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")
Community
  • 1
  • 1
Coolcrab
  • 2,655
  • 9
  • 39
  • 59

2 Answers2

2

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.

will
  • 10,260
  • 6
  • 46
  • 69
0

I had the exact same problem as you. I added the following line and was able to rotate the custom x-axis tick labels:

ax1.axis["bottom"].major_ticklabels.set_axis_direction("left")

I found that little bit of code on this page of the documentation - see the "Rotation and Alignment of TickLabels" section: http://matplotlib.org/mpl_toolkits/axes_grid/users/axisartist.html

I also stumbled across a devdocs page, that says that most of the typical tick-related mpl code doesn't really work with the mpl_toolkits.axisartist implementation.

A similar warning is at the top of the AxisGrid documentation: http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html?highlight=host_subplot

SJH
  • 1