Adjusting width of single data barplot - python

I am automating making reports based on particular locations. Certain graphs contain bar graphs with only one bar and they span the width of the graph. I tried using axes.set_xlim(0, len(data)-1) but ended up with the graph looking like the top one in the attached picture, if there are more than one bars. When I have multiple bars, usually leaving out axes.set_xlim gives a decent graph (last graph in picture). I know using width in the graph will only work is there are two or more bars.
I used len(data) since data is a list and the length would tell me how many bars are going to pop up.
My graph code is within a for loop, so each iteration will have a different length for data
Is there a way to adjust the widths accordingly?

Related

Matplotlib savefig bbox_inches='tight' along a single direction only?

I have two plots, each with multiple subplots (panels), i.e. multiple rows and columns. Each row shows the same type of data as images with colorbars on the right side. The first plot has, say, 8 rows and 4 columns. The second one has, say, 3 rows and 4 columns. The two plots are inserted into a LaTeX pdf document on two consecutive pages, with the same width (\includegraphics[width=\hsize]{fig1.pdf}). For layout reasons, I want the panels to have exactly the same width and height when flipping between pages in a pdf reader. To guarantee this, I thus used the same subplot layout of 8 x 4 panels for the second plot and made the panels (axes) for the 5 rows where there are no data invisible.
Since the second plot has only 3 rows of data, I use fig.savefig(bbox_inches='tight') to clip the white space below those rows. Unfortunately, as the ticks on the colorbar on the right side of the last column have a different maximum number of digits (on the first and second plot, say 1 and 2 decimal digits), with bbox_inches='tight' the resulting figure width (after saving and thus also when included into the LaTeX pdf) becomes different between the two plots.
I would like to not have to use a different layout of 3 x 4 subplots for the second plot, where I have to manually adjust the figure height (and likely still don't get exactly the same panel sizes). I could adjust both the tick formatters and tick locators on the colorbars to the same maximum number of digits for the two plots, but I would prefer to let matplotlib use the default ScalarFormatter() and AutoLocator().
Optimally, I would like to clip the white space below the 3 rows for the y-direction using bbox_inches='tight', but leave the bbox untouched for the x-direction and manually adjust the right figure border via fig.subplot_params(fig_right) to the same value for the two plots.
Questions: Is it possible to use bbox_inches='tight' only for the y-direction of a figure, but use the default bbox_inches (rcParams['savefig.bbox'], defaults to None) for the x-direction? Should such a feature (accepted values 'tight_x' and 'tight_y' for bbox_inches) maybe be implemented to matplotlib (I assume (but don't know) that this would not be too difficult)? Do you have alternative ideas how I can obtain exactly the same panel sizes for the two plots?
Update from original poster: As noted by #Jody Klymak, it is not possible to use a tight bounding box for one direction (or one border) only, but custom bounding boxes may be used. In my case, I pass bbox_inches=None for the first plot, and bbox_inches=Bbox([[0,fig.get_tightbbox(fig.canvas.get_renderer())._bbox.y0/fig.dpi - 0.1],[fig.get_figwidth(),fig.get_figheight()]]) for the second plot. This solves the question.

How can I graph a 3D timeseries of a dataset that contains multiple lines at different y-axis scales?

I am trying to create a 3D graph of a dataset that contains one x-axis (time_step), and about 8 y-axes at different scales. The reason I need to do so, is to show a significant change in the series that can be observed in all the different features at a particular point in time.
The issue I am running into is the fact that they y-axes are in different scales. For example, bonding_energy is around 1000, whereas potential is around-58000. What I would like to do is scale the y-axes around to place the lines somewhat parallel to one another.
Here is a sample of the dataset I am working with:
Here is an example of the image I am trying to create:

Plotting multiple Figures with Plot.ly

I have a plot that graphs multiple datasets against time, over several different generations. A slider controls the generation. The way I'm accomplishing this right now is by making (Number of Generations * Number of Lines) scatter plots in a list that I pass in to plotly.offline.plot. This makes creating the generation slider pretty difficult and full of annoying bugs (like the legend changing order for each generation).
My question is this: Is there some way I can group multiple scatter plots (like with a Figure) and pass in a list of those groups in to plotly.offline.plot?

Spacing Between Subplots in Matplotlib Python

I have subplots that come in pairs and then are stacked. I have them dynamically build based on how many runs I do (2 subplots side by side per run).
These subplots have titles on top (default position).
What I am running into is that the second pair's title, which resides under the first pair, is over lapping with the x-axis of the first pair.
I have tried tight_layout however it squished my graphs to the point of them being unusable.
I am looking for a way to stack pairs of subplots with enough room inbetween to have titles. Is there any way to add padding in between the stacked pairs. I also tried subplots_adjust(bottom=0.2) but that didn't work either.
I figured this would be a lot easier.

Python: how to plot points with little overlapping

I am using python to plot points. The plot shows relationship between area and the # of points of interest (POIs) in this area. I have 3000 area values and 3000 # of POI values.
Now the plot looks like this:
The problem is that, at lower left side, points are severely overlapping each other so it is hard to get enough information. Most areas are not that big and they don't have many POIs.
I want to make a plot with little overlapping. I am wondering whether I can use unevenly distributed axis or use histogram to make a beautiful plot. Can anyone help me?
I would suggest using a logarithmic scale for the y axis. You can either use pyplot.semilogy(...) or pyplot.yscale('log') (http://matplotlib.org/api/pyplot_api.html).
Note that points where area <= 0 will not be rendered.
I think we have two major choices here. First adjusting this plot, and second choosing to display your data in another type of plot.
In the first option, I would suggest clipping the boundries. You have plenty of space around the borders. If you limit the plot to the boundries, your data would scale better. On top of it, you may choose to plot the points with smaller dots, so that they would seem less overlapping.
Second option would be to choose displaying data in a different view, such as histograms. This might give a better insight in terms of distribution of your data among different bins. But this would be completely different type of view, in regards to the former plot.
I would suggest trying to adjust the plot by limiting the boundries of the plot to the data points, so that the plot area would have enough space to scale the data and try using histograms later. But as I mentioned, these are two different things and would give different insights about your data.
For adjusting you might try this:
x1,x2,y1,y2 = plt.axis()
plt.axis((x1,x2,y1,y2))
You would probably need to make minor adjustments to the axis variables. Note that there should definetly be better options instead of this, but this was the first thing that came to my mind.

Categories