I'm having the hardest time achieving the following:
I need to stop the x labels from showing after the vertical dashed line seen in the image:
Basically, I want to eliminate the 5 in this example.
I successfully stopped the ticks with the condition placed on "set_ticks", but the labels keep displaying.
My code:
ax2 = plt.subplot()
ax2.pcolormesh(xext, ye, Zext, cmap='hot')
ax2.set_xticks([a for a in xext if a<myval], minor=True)
ax2.axvline(myval, linestyle='--', color='white')
plt.show()
A solution like writing a list of [1,2,3,4] would not help me.
I need this to work for a large number of examples where all I know is the limit value, or myval from the code above.
(I am restating and narrowing down a question I posted before, now deleted.)
You only changed the minor ticks (the very short lines) on the x-axis. The major ticks also should be changed. ax.get_xticks() gives a list of the current ticks, which you can filter and apply again:
import matplotlib.pyplot as plt
import numpy as np
fig, ax2 = plt.subplots()
xext = np.arange(9.1, step=.1)
ye = np.arange(6)
zext = np.random.randn(len(ye) - 1, len(xext) - 1).cumsum(axis=1)
zext -= zext.mean(axis=1, keepdims=True)
ax2.pcolormesh(xext, ye, zext, cmap='hot')
myval = 6.28
ax2.set_xticks([a for a in xext if a < myval], minor=True)
xticks = ax2.get_xticks()
ax2.set_xticks([a for a in xticks if a < myval])
ax2.axvline(myval, linestyle='--', color='white')
plt.show()
Related
I am having an issue trying to get my date ticks rotated in matplotlib. A small sample program is below. If I try to rotate the ticks at the end, the ticks do not get rotated. If I try to rotate the ticks as shown under the comment 'crashes', then matplot lib crashes.
This only happens if the x-values are dates. If I replaces the variable dates with the variable t in the call to avail_plot, the xticks(rotation=70) call works just fine inside avail_plot.
Any ideas?
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
def avail_plot(ax, x, y, label, lcolor):
ax.plot(x,y,'b')
ax.set_ylabel(label, rotation='horizontal', color=lcolor)
ax.get_yaxis().set_ticks([])
#crashes
#plt.xticks(rotation=70)
ax2 = ax.twinx()
ax2.plot(x, [1 for a in y], 'b')
ax2.get_yaxis().set_ticks([])
ax2.set_ylabel('testing')
f, axs = plt.subplots(2, sharex=True, sharey=True)
t = np.arange(0.01, 5, 1)
s1 = np.exp(t)
start = dt.datetime.now()
dates=[]
for val in t:
next_val = start + dt.timedelta(0,val)
dates.append(next_val)
start = next_val
avail_plot(axs[0], dates, s1, 'testing', 'green')
avail_plot(axs[1], dates, s1, 'testing2', 'red')
plt.subplots_adjust(hspace=0, bottom=0.3)
plt.yticks([0.5,],("",""))
#doesn't crash, but does not rotate the xticks
#plt.xticks(rotation=70)
plt.show()
If you prefer a non-object-oriented approach, move plt.xticks(rotation=70) to right before the two avail_plot calls, eg
plt.xticks(rotation=70)
avail_plot(axs[0], dates, s1, 'testing', 'green')
avail_plot(axs[1], dates, s1, 'testing2', 'red')
This sets the rotation property before setting up the labels. Since you have two axes here, plt.xticks gets confused after you've made the two plots. At the point when plt.xticks doesn't do anything, plt.gca() does not give you the axes you want to modify, and so plt.xticks, which acts on the current axes, is not going to work.
For an object-oriented approach not using plt.xticks, you can use
plt.setp( axs[1].xaxis.get_majorticklabels(), rotation=70 )
after the two avail_plot calls. This sets the rotation on the correct axes specifically.
Solution works for matplotlib 2.1+
There exists an axes method tick_params that can change tick properties. It also exists as an axis method as set_tick_params
ax.tick_params(axis='x', rotation=45)
Or
ax.xaxis.set_tick_params(rotation=45)
As a side note, the current solution mixes the stateful interface (using pyplot) with the object-oriented interface by using the command plt.xticks(rotation=70). Since the code in the question uses the object-oriented approach, it's best to stick to that approach throughout. The solution does give a good explicit solution with plt.setp( axs[1].xaxis.get_majorticklabels(), rotation=70 )
An easy solution which avoids looping over the ticklabes is to just use
fig.autofmt_xdate()
This command automatically rotates the xaxis labels and adjusts their position. The default values are a rotation angle 30° and horizontal alignment "right". But they can be changed in the function call
fig.autofmt_xdate(bottom=0.2, rotation=30, ha='right')
The additional bottom argument is equivalent to setting plt.subplots_adjust(bottom=bottom), which allows to set the bottom axes padding to a larger value to host the rotated ticklabels.
So basically here you have all the settings you need to have a nice date axis in a single command.
A good example can be found on the matplotlib page.
Another way to applyhorizontalalignment and rotation to each tick label is doing a for loop over the tick labels you want to change:
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
now = dt.datetime.now()
hours = [now + dt.timedelta(minutes=x) for x in range(0,24*60,10)]
days = [now + dt.timedelta(days=x) for x in np.arange(0,30,1/4.)]
hours_value = np.random.random(len(hours))
days_value = np.random.random(len(days))
fig, axs = plt.subplots(2)
fig.subplots_adjust(hspace=0.75)
axs[0].plot(hours,hours_value)
axs[1].plot(days,days_value)
for label in axs[0].get_xmajorticklabels() + axs[1].get_xmajorticklabels():
label.set_rotation(30)
label.set_horizontalalignment("right")
And here is an example if you want to control the location of major and minor ticks:
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
fig, axs = plt.subplots(2)
fig.subplots_adjust(hspace=0.75)
now = dt.datetime.now()
hours = [now + dt.timedelta(minutes=x) for x in range(0,24*60,10)]
days = [now + dt.timedelta(days=x) for x in np.arange(0,30,1/4.)]
axs[0].plot(hours,np.random.random(len(hours)))
x_major_lct = mpl.dates.AutoDateLocator(minticks=2,maxticks=10, interval_multiples=True)
x_minor_lct = matplotlib.dates.HourLocator(byhour = range(0,25,1))
x_fmt = matplotlib.dates.AutoDateFormatter(x_major_lct)
axs[0].xaxis.set_major_locator(x_major_lct)
axs[0].xaxis.set_minor_locator(x_minor_lct)
axs[0].xaxis.set_major_formatter(x_fmt)
axs[0].set_xlabel("minor ticks set to every hour, major ticks start with 00:00")
axs[1].plot(days,np.random.random(len(days)))
x_major_lct = mpl.dates.AutoDateLocator(minticks=2,maxticks=10, interval_multiples=True)
x_minor_lct = matplotlib.dates.DayLocator(bymonthday = range(0,32,1))
x_fmt = matplotlib.dates.AutoDateFormatter(x_major_lct)
axs[1].xaxis.set_major_locator(x_major_lct)
axs[1].xaxis.set_minor_locator(x_minor_lct)
axs[1].xaxis.set_major_formatter(x_fmt)
axs[1].set_xlabel("minor ticks set to every day, major ticks show first day of month")
for label in axs[0].get_xmajorticklabels() + axs[1].get_xmajorticklabels():
label.set_rotation(30)
label.set_horizontalalignment("right")
Simply use
ax.set_xticklabels(label_list, rotation=45)
I am clearly late but there is an official example which uses
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
to rotate the labels while keeping them correctly aligned with the ticks, which is both clean and easy.
Ref: https://matplotlib.org/stable/gallery/images_contours_and_fields/image_annotated_heatmap.html
fig,ax = plt.subplots(figsize=(16,4))
plt.autoscale(False)
xtickmin = np.arange(0,490001,2500)
xtickmaj = np.arange(0,490001,100000)
ytickmaj = np.arange(560,200,50)
ytickmin = np.arange(560,200,10)
ax.set_xlim(0,495000)
ax.set_ylim(200,560)
ax.set_xticks(xtickmaj)
ax.set_xticks(xtickmin, minor=True)
ax.set_yticks(ytickmaj)
ax.set_yticks(ytickmin, minor=True)
print(ax.get_xticks())
plt.grid(which="major", alpha=1)
plt.grid(which="minor", alpha=0.33)
#plt.minorticks_on()
plt.tight_layout()
plt.plot(plotresult)
plt.savefig("graph.png", dpi=600)
plt is matplotlib.pyplot and plotresult is a list with the values I want to graph.
Even though I set the y ticks with ax.set_yticks() it does not appear.
Doing the same with x-ticks works fine. Another evidence for this is that ax.get_yticks() returns an empty list whereas ax.get_xticks() returns the ticks as expected.
Also, plt.minorticks_on() actually turns off the minor ticks for some bizarre reason, so it's commented out.
I have the following matplotlib
I would like to divide x-ticks into 2 lines instead of 1 because sometimes they are so long that is why they come over another and then it is impossible to read x-ticks.
KEEP IN MIND X-ticks are not hard coded and they are changing. So not always same x-ticks.
So for following example it would be good if I have instead of to Schleswig-Holstein I could have:
to Schleswig-
Holstein
How would I put the string after - in newline for the x ticks? or simply after lets say 10 letters I wanna go to a new line
Btw it would be also good if I could center all the text like the example above
So following is also okay but not the best.
to Schleswig-
Holstein
PS: Here is the code I use:
# create figure
fig = plt.figure()
# x-Axis (sites)
i = np.array(i)
i_pos = np.arange(len(i))
# y-Axis (values)
u = urbs_values
o = oemof_values
plt.bar(i_pos-0.15, list(u.values()), label='urbs', align='center', alpha=0.75, width=0.2)
plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
plt.bar(i_pos+0.15, list(o.values()), label='oemof', align='center', alpha=0.75, width=0.2)
plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
# tick names
plt.xticks(i_pos, list(map((' to ').__add__, list(u.keys()))))
# plot specs
plt.xlabel('Lines')
plt.ylabel('Capacity [MW]')
plt.title(site+' '+name)
plt.grid(True)
plt.legend()
plt.ticklabel_format(style='sci', axis='y')
# plt.show()
# save plot
fig.savefig(os.path.join(result_dir, 'comp_'+name+'_'+site+'.png'), dpi=300)
plt.close(fig)
You can use re as suggested on this answer and create a list of new labels with a new line character after every 10th character.
import re
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
xlabels = ["to Schleswig-Holstein", "to Mecklenburg-Vorpommern", r"to Lower Saxony"]
xlabels_new = [re.sub("(.{10})", "\\1\n", label, 0, re.DOTALL) for label in xlabels]
plt.plot(range(3))
plt.xticks(range(3), xlabels_new)
plt.show()
Alternative
xlabels_new = [label.replace('-', '-\n') for label in xlabels]
I have a python matplotlib graph showing up as below.
There are over 100 items on the X-axis and I DO want to plot them all, but want only about 25 or so (maybe automatically) so that it is clear to look at.
Can you please help?
Thanks
My code is also as follows:
l1 = plt.plot(b)
plt.setp(l1, linewidth=4, color='r')
l2 = plt.plot(c)
plt.setp(l2, linewidth=4, color='k')
l3 = plt.plot(d)
plt.setp(l3, linewidth=4, color='g')
plt.xticks(range(len(a)), a)
plt.xticks(rotation=30)
plt.show()
plt.savefig('a.png')
NOTE: I also have the data column a (the X-axis variable) in the form
u' 2016-02-29T00:01:30.000Z CHEPSTLC0007143 CDC-R114-DK'
which throws this error invalid literal for float(). That is the reason I am using plt.xticks(range(len(a)), a).
This is a case where mpl is doing exactly what you told it to, but what you told it to do is sort of inconvenient.
plt.xticks(range(len(a)), a)
is telling mpl to put a tick at every integer and to use the strings in a to label the ticks (which it is correctly doing). I think instead you want to be doing something like
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
# synthetic data
a = list(range(45))
d = ['the label {}'.format(i) for i in range(45)]
# make figure + axes
fig, ax = plt.subplots(tight_layout=True)
ax.set_xlabel('x label')
ax.set_ylabel('y label')
# draw one line
ln1, = ax.plot(range(45), lw=4, color='r')
# helper function for the formatter
def listifed_formatter(x, pos=None):
try:
return d[int(x)]
except IndexError:
return ''
# make and use the formatter
mt = mticker.FuncFormatter(listifed_formatter)
ax.xaxis.set_major_formatter(mt)
# set the default ticker to only put ticks on the integers
loc = ax.xaxis.get_major_locator()
loc.set_params(integer=True)
# rotate the labels
[lab.set_rotation(30) for lab in ax.get_xticklabels()]
If you pan/zoom the ticklabels will be correct and mpl will select a sensible number of ticks to show.
[side note, this output is from the 2.x branch and shows some of the new default styling]
Just replace plt.xticks(range(len(a)), a) by plt.xticks(np.arange(0, len(a) + 1, 5)) and you are gonna reduce the number of x axis labels displayed.
If you want to show only 3 ticks, use the following code:
axes = plt.axes()
x_values = axes.get_xticks()
y_values = axes.get_yticks()
x_len = len(x_values)
y_len = len(y_values)
print(x_len)
print(y_len)
new_x = [x_values[i] for i in [0, x_len // 2, -1]]
new_y = [y_values[i] for i in [0, y_len // 2, -1]]
axes.set_xticks(new_x)
axes.set_yticks(new_y)
Similarly, if you want to show only 25 ticks, just pick up equally spaced 25 values from your get_xticks()
I'm familiar with the following questions:
Matplotlib savefig with a legend outside the plot
How to put the legend out of the plot
It seems that the answers in these questions have the luxury of being able to fiddle with the exact shrinking of the axis so that the legend fits.
Shrinking the axes, however, is not an ideal solution because it makes the data smaller making it actually more difficult to interpret; particularly when its complex and there are lots of things going on ... hence needing a large legend
The example of a complex legend in the documentation demonstrates the need for this because the legend in their plot actually completely obscures multiple data points.
http://matplotlib.sourceforge.net/users/legend_guide.html#legend-of-complex-plots
What I would like to be able to do is dynamically expand the size of the figure box to accommodate the expanding figure legend.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-2*np.pi, 2*np.pi, 0.1)
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot(x, np.sin(x), label='Sine')
ax.plot(x, np.cos(x), label='Cosine')
ax.plot(x, np.arctan(x), label='Inverse tan')
lgd = ax.legend(loc=9, bbox_to_anchor=(0.5,0))
ax.grid('on')
Notice how the final label 'Inverse tan' is actually outside the figure box (and looks badly cutoff - not publication quality!)
Finally, I've been told that this is normal behaviour in R and LaTeX, so I'm a little confused why this is so difficult in python... Is there a historical reason? Is Matlab equally poor on this matter?
I have the (only slightly) longer version of this code on pastebin http://pastebin.com/grVjc007
Sorry EMS, but I actually just got another response from the matplotlib mailling list (Thanks goes out to Benjamin Root).
The code I am looking for is adjusting the savefig call to:
fig.savefig('samplefigure', bbox_extra_artists=(lgd,), bbox_inches='tight')
#Note that the bbox_extra_artists must be an iterable
This is apparently similar to calling tight_layout, but instead you allow savefig to consider extra artists in the calculation. This did in fact resize the figure box as desired.
import matplotlib.pyplot as plt
import numpy as np
plt.gcf().clear()
x = np.arange(-2*np.pi, 2*np.pi, 0.1)
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot(x, np.sin(x), label='Sine')
ax.plot(x, np.cos(x), label='Cosine')
ax.plot(x, np.arctan(x), label='Inverse tan')
handles, labels = ax.get_legend_handles_labels()
lgd = ax.legend(handles, labels, loc='upper center', bbox_to_anchor=(0.5,-0.1))
text = ax.text(-0.2,1.05, "Aribitrary text", transform=ax.transAxes)
ax.set_title("Trigonometry")
ax.grid('on')
fig.savefig('samplefigure', bbox_extra_artists=(lgd,text), bbox_inches='tight')
This produces:
[edit] The intent of this question was to completely avoid the use of arbitrary coordinate placements of arbitrary text as was the traditional solution to these problems. Despite this, numerous edits recently have insisted on putting these in, often in ways that led to the code raising an error. I have now fixed the issues and tidied the arbitrary text to show how these are also considered within the bbox_extra_artists algorithm.
Added: I found something that should do the trick right away, but the rest of the code below also offers an alternative.
Use the subplots_adjust() function to move the bottom of the subplot up:
fig.subplots_adjust(bottom=0.2) # <-- Change the 0.02 to work for your plot.
Then play with the offset in the legend bbox_to_anchor part of the legend command, to get the legend box where you want it. Some combination of setting the figsize and using the subplots_adjust(bottom=...) should produce a quality plot for you.
Alternative:
I simply changed the line:
fig = plt.figure(1)
to:
fig = plt.figure(num=1, figsize=(13, 13), dpi=80, facecolor='w', edgecolor='k')
and changed
lgd = ax.legend(loc=9, bbox_to_anchor=(0.5,0))
to
lgd = ax.legend(loc=9, bbox_to_anchor=(0.5,-0.02))
and it shows up fine on my screen (a 24-inch CRT monitor).
Here figsize=(M,N) sets the figure window to be M inches by N inches. Just play with this until it looks right for you. Convert it to a more scalable image format and use GIMP to edit if necessary, or just crop with the LaTeX viewport option when including graphics.
Here is another, very manual solution. You can define the size of the axis and paddings are considered accordingly (including legend and tickmarks). Hope it is of use to somebody.
Example (axes size are the same!):
Code:
#==================================================
# Plot table
colmap = [(0,0,1) #blue
,(1,0,0) #red
,(0,1,0) #green
,(1,1,0) #yellow
,(1,0,1) #magenta
,(1,0.5,0.5) #pink
,(0.5,0.5,0.5) #gray
,(0.5,0,0) #brown
,(1,0.5,0) #orange
]
import matplotlib.pyplot as plt
import numpy as np
import collections
df = collections.OrderedDict()
df['labels'] = ['GWP100a\n[kgCO2eq]\n\nasedf\nasdf\nadfs','human\n[pts]','ressource\n[pts]']
df['all-petroleum long name'] = [3,5,2]
df['all-electric'] = [5.5, 1, 3]
df['HEV'] = [3.5, 2, 1]
df['PHEV'] = [3.5, 2, 1]
numLabels = len(df.values()[0])
numItems = len(df)-1
posX = np.arange(numLabels)+1
width = 1.0/(numItems+1)
fig = plt.figure(figsize=(2,2))
ax = fig.add_subplot(111)
for iiItem in range(1,numItems+1):
ax.bar(posX+(iiItem-1)*width, df.values()[iiItem], width, color=colmap[iiItem-1], label=df.keys()[iiItem])
ax.set(xticks=posX+width*(0.5*numItems), xticklabels=df['labels'])
#--------------------------------------------------
# Change padding and margins, insert legend
fig.tight_layout() #tight margins
leg = ax.legend(loc='upper left', bbox_to_anchor=(1.02, 1), borderaxespad=0)
plt.draw() #to know size of legend
padLeft = ax.get_position().x0 * fig.get_size_inches()[0]
padBottom = ax.get_position().y0 * fig.get_size_inches()[1]
padTop = ( 1 - ax.get_position().y0 - ax.get_position().height ) * fig.get_size_inches()[1]
padRight = ( 1 - ax.get_position().x0 - ax.get_position().width ) * fig.get_size_inches()[0]
dpi = fig.get_dpi()
padLegend = ax.get_legend().get_frame().get_width() / dpi
widthAx = 3 #inches
heightAx = 3 #inches
widthTot = widthAx+padLeft+padRight+padLegend
heightTot = heightAx+padTop+padBottom
# resize ipython window (optional)
posScreenX = 1366/2-10 #pixel
posScreenY = 0 #pixel
canvasPadding = 6 #pixel
canvasBottom = 40 #pixel
ipythonWindowSize = '{0}x{1}+{2}+{3}'.format(int(round(widthTot*dpi))+2*canvasPadding
,int(round(heightTot*dpi))+2*canvasPadding+canvasBottom
,posScreenX,posScreenY)
fig.canvas._tkcanvas.master.geometry(ipythonWindowSize)
plt.draw() #to resize ipython window. Has to be done BEFORE figure resizing!
# set figure size and ax position
fig.set_size_inches(widthTot,heightTot)
ax.set_position([padLeft/widthTot, padBottom/heightTot, widthAx/widthTot, heightAx/heightTot])
plt.draw()
plt.show()
#--------------------------------------------------
#==================================================
I tried a very simple way, just make the figure a bit wider:
fig, ax = plt.subplots(1, 1, figsize=(a, b))
adjust a and b to a proper value such that the legend is included in the figure