showing specific xtick in matplotlib - python

So I have a graph that runs on an order of magnitude 10000 time steps, and thus I have a lot of data points and the xticks are spaced pretty far apart, which is cool, but I would like to to show on the xaxis the point at which the data is being plotted. In this case the xtick I want to show is 271. So is there a way to just "insert" 271 tick onto the x axis given that I already know what tick I want to display?

If it's not important that the ticks update when panning/zomming (i.e. if the plot is not meant for interactive use), then you can manually set the tick locations with the axes.set_xticks() method. In order to append one location (e.g. 271), you can first get the current tick locations with axes.get_xticks(), and then append 271 to this array.
A short example:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.arange(300))
# Get current tick locations and append 271 to this array
x_ticks = np.append(ax.get_xticks(), 271)
# Set xtick locations to the values of the array `x_ticks`
ax.set_xticks(x_ticks)
plt.show()
This produces
As you can see from the image, a tick has been added for x=271.

Related

How to avoid congestion of ticks in y axis?

The figure attached here has too many ticks in y axis and is congested.I dont want to change the y axis to any other scale.I want to hide the every second number in y axis.Is this possible?,which means [5,15,25.....] will be hidden.
Is there any other approach to avoid congestion in y axis?
pl.figure(figsize=(10, 8))
pl.scatter(x=T1['Current_Sim_rcs_obj1'], y=T1['Final Mean_Range'])
pl.xlabel('Truth RCS [dBsm]')
pl.xlim(-40, 5)
pl.ylim(0, 280)
pl.grid()
pl.ylabel('RUT_Range[m]')
pl.xticks(np.arange(-45, 15, 5))
pl.yticks(np.arange(0, 281, 10))
pl.show()
I tried to add the below line to the above code,which didn't worked as expected.
pl.axes().yaxis.set_minor_locator(MultipleLocator(5))
You can use matplotlib's yticks() function to get all the ticks (locations) and labels. Then you can modify the two lists based on some criteria. For the criteria you give, we can ignore every second tick and label:
from matplotlib import pyplot as plt
plt.figure() # Create a figure
locs, labels = plt.yticks() # Get the current locations and labels
locs = locs[0::2] # Choose every other location
labels = labels[0::2] # Choose every other label
plt.yticks(locs, labels) # Set new yticks and labels
I could imagine other cases where where the criteria are based on the length of locs. In fact, that's probably what matplotlib is doing behind the scenes already. It has some heuristic which is trying to choose a good length for locs based on the size of the plot and the scale of the data.

Show a (discrete) colorbar next to a plot as a legend for the (automatically chosen) line colors

I tried to make a plot showing many lines, but it is hard to tell them apart. They have different colors, but I would like to make it easy to show which line is which. A normal legend does not really work so well, since I have more than 10 lines.
The lines follow a logical sequence. I would like to (1) have their color automatically chosen from a colormaps (preferably one that has a smooth ordering, such as viridis or a rainbow). Then I would like (2) to have the tick marks next to the color bar to correspond to the index i for each line (or better a text label from an array of strings textlabels[i]).
Here's a minimal piece of code (with some gaps where I am not sure what to use). I hope this illustrates what I am trying.
import numpy as np
import matplotlib.pyplot as plt
# Genereate some values to plot on the x-axis
x = np.linspace(0,1,1000)
# Some code to select a (discrete version of) a rainbow/viridis color map
...
# Loop over lines that should appear in the plot
for i in range(0,9):
# Plot something (using straight lines with different slope as example)
plt.plot(i*x)
# Some code to plot a discrete color bar next
# to the plot with ticks showing the value of i
...
I currently have this. I would like the color bar to have the ticks with values of i, i.e. 0, 1, 2, ... next to it as tick marks.
Example figure of what I have now. It is hard to tell the lines apart now.
One gets a colormap via plt.get_cmap("name of cmap", number_of_colors).
This colormap can be used to compute the colors for the plots. It can also be used to generate a colorbar.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
n = 10 # how many lines to draw or number of discrete color levels
x = np.linspace(0,1,17)
cmap = plt.get_cmap("viridis", n)
for i in range(0,n):
plt.plot(i*x, color=cmap(i))
norm= matplotlib.colors.BoundaryNorm(np.arange(0,n+1)-0.5, n)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
plt.colorbar(sm, ticks=np.arange(0,n))
plt.show()

Python Matplotlib: Dual y-axis with same tick spacing and different scale [duplicate]

I created a matplotlib plot that has 2 y-axes. The y-axes have different scales, but I want the ticks and grid to be aligned. I am pulling the data from excel files, so there is no way to know the max limits beforehand. I have tried the following code.
# creates double-y axis
ax2 = ax1.twinx()
locs = ax1.yaxis.get_ticklocs()
ax2.set_yticks(locs)
The problem now is that the ticks on ax2 do not have labels anymore. Can anyone give me a good way to align ticks with different scales?
Aligning the tick locations of two different scales would mean to give up on the nice automatic tick locator and set the ticks to the same positions on the secondary axes as on the original one.
The idea is to establish a relation between the two axes scales using a function and set the ticks of the second axes at the positions of those of the first.
import matplotlib.pyplot as plt
import matplotlib.ticker
fig, ax = plt.subplots()
# creates double-y axis
ax2 = ax.twinx()
ax.plot(range(5), [1,2,3,4,5])
ax2.plot(range(6), [13,17,14,13,16,12])
ax.grid()
l = ax.get_ylim()
l2 = ax2.get_ylim()
f = lambda x : l2[0]+(x-l[0])/(l[1]-l[0])*(l2[1]-l2[0])
ticks = f(ax.get_yticks())
ax2.yaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks))
plt.show()
Note that this is a solution for the general case and it might result in totally unreadable labels depeding on the use case. If you happen to have more a priori information on the axes range, better solutions may be possible.
Also see this question for a case where automatic tick locations of the first axes is sacrificed for an easier setting of the secondary axes tick locations.
To anyone who's wondering (and for my future reference), the lambda function f in ImportanceofBeingErnest's answer maps the input left tick to a corresponding right tick through:
RHS tick = Bottom RHS tick + (% of LHS range traversed * RHS range)
Refer to this question on tick formatting to truncate decimal places:
from matplotlib.ticker import FormatStrFormatter
ax2.yaxis.set_major_formatter(FormatStrFormatter('%.2f')) # ax2 is the RHS y-axis

autofmt_xdate deletes x-axis labels of all subplots

I use autofmt_xdate to plot long x-axis labels in a readable way. The problem is, when I want to combine different subplots, the x-axis labeling of the other subplots disappears, which I do not appreciate for the leftmost subplot in the figure below (two rows high). Is there a way to prevent autofmt_xdate from quenching the other x-axis labels? Or is there another way to rotate the labels? As you can see I experimented with xticks and "rotate" as well, but the results were not satisfying because the labels were rotated around their center, which resulted in messy labeling.
Script that produces plot below:
from matplotlib import pyplot as plt
from numpy import arange
import numpy
from matplotlib import rc
rc("figure",figsize=(15,10))
#rc('figure.subplot',bottom=0.1,hspace=0.1)
rc("legend",fontsize=16)
fig = plt.figure()
Test_Data = numpy.random.normal(size=20)
fig = plt.figure()
Dimension = (2,3)
plt.subplot2grid(Dimension, (0,0),rowspan=2)
plt.plot(Test_Data)
plt.subplot2grid(Dimension, (0,1),colspan=2)
for i,j in zip(Test_Data,arange(len(Test_Data))):
plt.bar(i,j)
plt.legend(arange(len(Test_Data)))
plt.subplot2grid(Dimension, (1,1),colspan=2)
xticks = [r"%s (%i)" % (a,b) for a,b in zip(Test_Data,Test_Data)]
plt.xticks(arange(len(Test_Data)),xticks)
fig.autofmt_xdate()
plt.ylabel(r'$Some Latex Formula/Divided by some Latex Formula$',fontsize=14)
plt.plot(Test_Data)
#plt.setp(plt.xticks()[1],rotation=30)
plt.tight_layout()
#plt.show()
This is actually a feature of the autofmt_xdate method. From the documentation of the autofmt_xdate method:
Date ticklabels often overlap, so it is useful to rotate them and right align them. Also, a common use case is a number of subplots with shared xaxes where the x-axis is date data. The ticklabels are often long, and it helps to rotate them on the bottom subplot and turn them off on other subplots, as well as turn off xlabels.
If you want to rotate the xticklabels of the bottom right subplot only, use
plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment
This rotates the ticklabels 30 degrees and right aligns them (same result as when using autofmt_xdate) for the bottom right subplot, leaving the two other subplots unchanged.

Matplotlib: Add strings as custom x-ticks but also keep existing (numeric) tick labels? Alternatives to matplotlib.pyplot.annotate?

I am trying to produce a graph and I am having some issues annotating it.
My graph has a log scale on the x-axis, showing time. What I want to be able to do is keep the existing (but not predictable) numeric tick labels at 100 units, 1000 units, 10000 units, etc but also add custom tick labels to the x-axis that make it clear where more "human readable" time intervals occur---for instance I want to be able to label 'one week', 'one month', '6 months', etc.
I can use matplotlib.pyplot.annotate() to mark the points but it doesn't really do what I want. I don't really want text and arrows on top of my graph, I just want to add a few extra custom tick marks. Any ideas?
If you really want to add extra ticks, you can get the existing ones using axis.xaxis.get_majorticklocs(), add whatever you want to add, and then set the ticks using axis.xaxis.set_ticks(<your updated array>).
An alternative would be to add vertical lines using axvline. The advantage is that you don't have to worry about inserting your custom tick into the existing array, but you'll have to annotate the lines manually.
Yet another alternative would be to add a linked axis with your custom ticks.
From http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.xticks:
# return locs, labels where locs is an array of tick locations and
# labels is an array of tick labels.
locs, labels = xticks()
So all you should need to do is obtain the locs and labels and then modify labels to your liking (dummy example):
labels = ['{0} (1 day)','{0} (1 weak)', '{0} (1 year)']
new_labels = [x.format(locs[i]) for i,x in enumerate(labels)]
and then run:
xticks(locs, new_labels)
This is my solution. The main advantages are:
You can specify the axes (useful for twin axes or if working with multiple axes simultaneously)
You can specify the axis (put ticks on x-axis or y-axis)
You can easily add new ticks while keeping the automatic ones
It automatically replaces if you add a tick that already exists.
Code:
#!/usr/bin/python
from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
#Function to add ticks
def addticks(ax,newLocs,newLabels,pos='x'):
# Draw to get ticks
plt.draw()
# Get existing ticks
if pos=='x':
locs = ax.get_xticks().tolist()
labels=[x.get_text() for x in ax.get_xticklabels()]
elif pos =='y':
locs = ax.get_yticks().tolist()
labels=[x.get_text() for x in ax.get_yticklabels()]
else:
print("WRONG pos. Use 'x' or 'y'")
return
# Build dictionary of ticks
Dticks=dict(zip(locs,labels))
# Add/Replace new ticks
for Loc,Lab in zip(newLocs,newLabels):
Dticks[Loc]=Lab
# Get back tick lists
locs=list(Dticks.keys())
labels=list(Dticks.values())
# Generate new ticks
if pos=='x':
ax.set_xticks(locs)
ax.set_xticklabels(labels)
elif pos =='y':
ax.set_yticks(locs)
ax.set_yticklabels(labels)
#Get numpy arrays
x=np.linspace(0,2)
y=np.sin(4*x)
#Start figure
fig = plt.figure()
ax=fig.add_subplot(111)
#Plot Arrays
ax.plot(x,y)
#Add a twin axes
axr=ax.twinx()
#Add more ticks
addticks(ax,[1/3,0.75,1.0],['1/3','3/4','Replaced'])
addticks(axr,[0.5],['Miguel'],'y')
#Save figure
plt.savefig('MWE.pdf')
I like Miguel's answer above. Worked quite well. However, a small adjustment has to be made. The following:
# Get back tick lists
locs=Dticks.keys()
labels=Dticks.values()
must be changed to
# Get back tick lists
locs=list(Dticks.keys())
labels=list(Dticks.values())
since, in Python 2.7+/3, Dict.keys() and Dict.values() return dict_keys and dict_values objects, which matplotlib does not like (apparently). More about those two objects in PEP 3106.

Categories