python - How can I increase the frequency of xticks/ labels for dates on a bar plot? -
I'm trying to increase the xtick and label frequency on the barlot, currently it labels every 2 months, And I extend it every month.
I have a code that is so far:
fig = plt.figure () ax = plt.subplot (1,1,1 ) Plot.subplots_adjust (left = 0.125, bottom = 0.1, right = 0.9, top = 0.9, wspace = 0.2, hspace = 0.35), defined plot spacing around # bar plot = bar1 = ax.bar (date, clim [ Hand for loc = [] #,:], label = 'climate (1981 - 2013)' , Color = 'darkgray', width = width_arr [,], linewidth = 0, alpha = 0.5) handles.append (bar1) bar2 = ax.bar (date, chirps [I, loc,:], label = str (year) 1), color = 'blue', width = width_ar [,], linewidth = 0, alpha = 0.45) handle .plend tick labels for tick in .append (bar2) # tick in ax.yaxis.get_major_ticks .label.set_fontsize (10) (): ax.xaxis.get_major_ticks () tick.label.set_fontsize (10) ax.set_ylabel ("rain", fontsize = 10) # plot plot plot space plt.legend (loc = 'upper The stories within the left ', numpoints = 1, ncol = 3, prop = {' size ': 10}) plt.ylim (0, 110) #Tick markers to make the best Using X-Axis Space Plate while rotating, .xticks (rotation = '25')
You can specify xtick loactions in the xticks
command, so in your case this should do the trick: Instead
plt.xticks (rotation = '25 ')
Usage
plt.xticks (date, rotation =' 25 ')
This is one that shows your questions and fixes
NMP import as import matplotlib.pyplot plt # generated random date X = [1.0, 2.0, 3.0, 4.0, 5.0] y = np.random.rand (2,5) # Plot fig = plt.figure () ax = plt.subplot (1,1,1) # of the plot Set the details, only the bar command is important: bar1 = ax.bar (x, y [0,:], color = 'darkgray', width = 0.5, linewidth = 0, alpha = 0.5) bar2 = ax.bar (x , Y [1,:], color = 'blue', width = 0.5, linewidth = 0, alpha = 0.45) #xticks plt.xticks (rotation = '25') define # plt.xticks (x, rotation = '25') plt.show ()
This will generate the following figure:
In this figure, we have many more labels Can indicate we than expected - which have, on the contrary, however, the answer is the same.
Now, if you change the plt.xticks
command (in the above example, you comment on one line and unconnect the other), this figure will change to the following:
The fact is that the y-data has changed ( Reason for the call of np.random
) is irrelevant. You can see that now we have x-ticks in the given places in x
If you want to leave some labels, then I would suggest using the function I am Contrary to Number
, linspace
, allows for a certain increase (third input argument). For example, with the code below you can replace the line plt.xticks (x, rotation = '25 ')
:
# xtxt names = ['One', 'two', 'three', 'four', 'five'] x_tick_location = np.arange (np.min (x), np.max (x) +0.001.2.0) x_tick_labels = [name [ X index (xi) x_tick_location] for xi in plt.xticks (x_tick_location, x_tick_labels, rotation = '25')
This will give you the following plot:
There are some things to look for:
< Ul> names
I have strongly recommended to set the labels in this way, because it separates your tick label from your data here, this xticks The tick code is defined using np.arange
in the form described above. x
between minimum
and maximum
entries, we increase this example from 2
. To ensure this, it may be possible to add a small amount in the maximum
value (it has to do with floating point arithmetic). This command creates a list by moving the [x_index (xi)] x_tick_location to xi
from each element to x_tick_location
, find it by clicking the original < Select the element in names
using the index in code> x
and using that code. xticks
order
Comments
Post a Comment