subplot with shared axis but different ticks and labels - matplotlib

I make a plot with different subplots (using gridspec.GridSpec). Two subplots share the same x-axis (sharex=ax1 in the definition of the second subplot).
However, as one subplot shows the indices of the chronologically sorted data, and the second subplot shows the corresponding decades, I want seperate ticks and labels for the x-axes of both plots. This seems not possible, a unique set of labels and ticks are assigned to both subplots. Until now, I can only:
use different x-axes and thus assign two sets of ticks and labels.
In that case, the axes are not alligned although
ax1.set_xlim([start, stop]) are similarly defined for both subplots
use a common x-axis and one set of ticks and labels
I do not find a solution for this on the internet. Is someone able to help? Thank you in advance!

Related

Matplotlib's Figure and Axes explanation

I am really pretty new to matplotlib, though I know that it can be very powerful.
I've been reading number of tutorials and examples and it's a real hassle to understand how does matplotlib's Figure and Axes work. I am illustrating, what I am trying to understand, with the attached figure.
I know how to create a figure instance of certain size in inches. However, what bothers me is how can I create subplots and then axes, within each subplot, with relative coordinates (bottom=0,left=0,top=1,right=1) as illustrated.
So, for example I want to create a "parent" plot area (say (6in,10in)). Then, I want to create two subplot areas, each with size (3in,3in), with 1in space from the top, 2in space between the two vertical subplot areas and 1in from bottom. Then, 1in space on the left and 2in space on the write. In the same time, I would like to be able to get the coordinates of the subplot areas with respect to the main plot area.
Then, inside the first subplot area, I'd like to create 2 axis instances, with Axis 1, having coordinates with respect to Subplot Area1 (0.1,0.7,0.7,0.2) and Axes 2 (0.1,0.2,0.7,0.5). And then of course I'd like to be able to plot on these axes e.g., ax1.plot()....
If you could provide a sample code to achieve that, then I can study it.
Your help will be very much appreciated!
a subplot and an Axes object are really the same thing. There is not really a "subplot" as you describe it in matplotlib. You can just create your three Axes objects using gridspec without the need to put them in your "subplots".
There are a few different ways to create Axes instances within your figure.
fig.add_axes will create an Axes instance at the position given to it (you give it [left,bottom,width,height] in figure coordinates (i.e. 0,0 is bottom left, 1,1 is top right).
fig.add_subplot will also create an Axes instance. In this case, rather than giving it a rectangle to be created in, you give it the number of rows and columns of subplots you would like, and then the plot_number, where plot_number starts at 1, increments across rows first and has a maximum of nrows * ncols.
For example, to create the top-left Axes in a grid of 2 row and 2 columns, you could do the following:
fig.add_subplot(2,2,1)
or the shorthand
fig.add_subplot(221)
There are some more customisable ways to create Axes as well, for example gridspec and subplot2grid which allow for easy creation of many subplots of different shapes and sizes.

How do create a scale for a second axis without unnecessary (or redundant) plotting?

I have a plot in which I have already plotted all my data and a "twined" axis, on which I'd like to use another scale, in this case dates. I also have a list of all the dates corresponding to each element of my data, and want to add an a scale for the dates to the twined axis.
For example, I have
ax2 = ax1.twinx()
and lists x_temporal_data, y_day_offsets, y_dates, all of the same length, and have already plotted the relationship between the first two with
ax1.plot(x_temporal_data, y_day_offsets)
and I just want to have a scale on ax2 for the dates in y_dates, since y_day_offsets and y_dates are "synonyms" for the same time information.
Is there a way to do this without "plotting" something I don't need to display (since all my data is already plotted). For example, I can get the dates to appear perfectly on ax2 with
ax2.plot(len(y_dates)*[some_random_out_of_xrange_value], y_dates)
but that seems like a hack: plotting nothing to "calibrate" the second axis.
Is there a better, more idiomatic way of accomplishing this?
Simply set the scale on the second y-axis to your liking with:
ax2.set_ylim([min(y_dates), max(y_dates)])

Reportlab LinePlot - how do I add a lineLegend...or label my lines?

I have a lineplot with 2 lines on it...they're two separate channels from the same data set. Would love to just label each one - the "labels" options are all about giving a number for each point on your plot, and that is simply not helpful.
Would love to know how to do any (really, all, but I just need to do one to be happy) of these:
plot each against its own y axis and be able to sensibly label that axis with units (and color the numbers to correspond to the data it correlates to)
put a legend on it. I can't figure out how to use lineLegend
just put any kind of (singular) label in the vicinity of the lines.

matplotlib one centered axis label for two diagrams

I'd like to have one axis label centered over two axes in matplotlib.
For example, I set up the axes as follows:
figure = pyplot.figure(figsize=10,10))
diagram1 = figure.add_axes([0.01,0.62,0.90,0.30])
diagram2 = figure.add_axes([0.01,0.32,0.90,0.30])
This will generate two diagrams on top of each other. How can i now define one axis label for the y axis centered on both diagrams.
I think this can be done with subplots, but I prefer to set every diagram individually, as shown above.
You can manually change the y-coordinate of the label.
diagram1.set_ylabel('y label').set_y(0)
# Alternatively you can use
# diagram2.set_ylabel('y label').set_y(1)
The coordinate is in the axes coordinate space, meaning 0 is the bottom and 1 is the top of the Axes.
Seeing how your Axes are placed at x=0.01, you can make the label appear at the right side of the plots as well
diagram1.yaxis.set_label_position("right")

Matplotlib:subplots fonts overlap

I want to make 21 subplots using the matplotlib. In some of the plots, fonts of axis are overlapping on the X-axis but X-axis labels are OK. For example, X-axis of one plot has value of (0,10,2) and adjoining plot has value of (0.001,0.010,0.002) so in subplots, 10 (on first X-axis) is overlap with 0.001 (X-axis of adjoining plot). If any one know about it then please reply me.
Many thanks
-Viral
A few things you can try:
Adjust subplots so that there is more wspace and hspace
Adjust xlim/ylim manually to make sure there's no overlap (tricky)
Use tight_layout (see this question)