Matplotlib bar plot remove internal lines - python

I have a bar plot with high resolution. Is it possible to have only the border/frame/top line of the plot like in the following ROOT plot without, i.e. without internal lines?
If I plot with facecolor='none' or 'white', the plot is slashed by both vertical and horizontal lines:
The only way I can get rid of them is to make edgecolor and facecolor the same, but that's not the look I need...

Found out the answer: the simplest way to achieve the desired look is to use plt.step instead of plt.bar, that simple. Feel shame for asking.

As referenced here, you can set histtype='step' to remove internal lines. For example,
plt.hist(data, histtype='step')

Related

How to adjust subplots borders in matplotlib automatically?

When plotting multiple plots using plt.subplots, most of the time the spacing between subplots is not ideal so the the xtick labels of the top plot would overlap with the title of the bottom plots. There is a way to fix this manually by calling say plt.subplots_adjust(hspace=0.5) and changing the parameters interactively to obtain a decent looking plot. Is there a way to calculate the subplot_adjust parameter automatically? Meaning finding the minimum hspace and wspace so that there is not overlap between texts of the plots.
You can use tight_layout https://matplotlib.org/stable/tutorials/intermediate/tight_layout_guide.html or constrained_layout https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html
I'm pretty certain that the closest your going to find to an inbuilt calculation method is:
plt.tight_layout()
or
figure.Figure.tight_layout() #if you are using the object version of the code

How to label the plot, using plot_date and bar in matplotlib?

ax4.plot_date(x=days, y=MACD_14_21,fmt="r-",label='MACD_14_21')
I am trying to print label using label="xxx", while plotting the data, but I don't see any label printed.
Do I need to use different syntax?
These labels are for legend only. Add plt.legend() to Your code to show it. If You want to add labels on the x-axis You can do it with plt.xlabel(['label1','label2']).
Edit:
Unhappily adding labels on plotted lines, is much more complex, and I would recommend You to stick to the legend, because there is no automatic way of doing that. However if You really need to achieve that, it's explained here: Print string over plotted line (mimic contour plot labels)
For simple cases You can also use annotations.

Matplotlib: Intelligent figure scale / legend location

Some code gives me the following matplotlib figure:
Unfortunately, the figure size is fixed and hence on the top right, the legend and the lines overlap. Is there any way to have the legend not stack on top of the lines?
I am aware that legend allows ax2.legend(loc=0), where 0 will put it into the "best" location. However, with two y axis as here, this will stack both legends on top of each other - not really the best allocation.
My next best try would be to "scale up" the figure, as manually done with an interactive graph, where I have only scaled up both axis:
Doing this with the "real" figure scale requires iterated "trying numbers and checking how far it goes" procedure - which may need to be redone if the graph changes. Is there any way of having matplotlib compute the scale "intelligently"?
If the best location plt.legend(loc='best') fails, try putting the legend outside of the plot:
plt.legend(loc='upper left', bbox_to_anchor=(1.02, 1), borderaxespad=0)
You can scale only legend, not the whole plot. Link here
More on legends here and also here.

matplotlib: enlarge axis-scale label

Is there a way to enlarge the axis-scale label in matplotlib (circled in red in the enlarged plot below)?
I've used ax.tick_params() to successfully edit the tick labels, but I haven't been able to find anything about this specific piece of the plot.
Worse comes to worst, I could go with a manual text() insertion, but I'd like something more direct if possible.
Add a line like this
ax.xaxis.get_children()[1].set_size(15)
To change your major tick scale label (I guess we can call it so) to 15 points, if you plot the plot on ax.
If you plot using the pyplot API, add a line of ax=plt.gca() as well.

Pyplot plot function

Given below is the code for plotting points using pyplot.
x1=300+p[k]*math.cos(val[thetaval])
y1=300+p[k]*math.sin(val[thetaval])
plt.plot(x1,y1,'k.')
The plotting is working fine, the problem is, if I want to plot it as a point I am specifying the dot in 'k.' inside the plot function. The output is something like:
The width of the black line/curve that I am plotting is much more that needed. How to reduce it?
It seems that you are not plotting a line but a series of small points. Maybe if you try setting the markersize argument of the plot function could work.
Looking into the documentation of plot() you can find "linewidth"
So use:
plt.plot(x1,y1,'k.', linewidth=0.1)

Categories