Pandas bar plot edge to dash - pandas

I have a bar plot and I can successfully turn the fill color to transparent. Now I'd like to change the edge to a dashed line, instead of a solid.
df.plot(kind='barh', x='state', y='population', color=[1,0,0,0], edgecolor='blue', width=0.5)
I've tried edgestyle, I've tried style='-' and '--'.
Is dash-ing only available for line plots?

df.plot(kind='barh', x='state', y='population', color=[1,0,0,0], edgecolor='blue', width=0.5, linestyle="--")

Related

How do I stretch our the horizontal axis of a matplotlib pyplot?

I'm creating a colour map which has 64 horizontal data points and 3072 vertical. When I plot it, the scaling on both axes is the same and so the horizontal axis is super squished and tiny, and I can't get any information from it. I've tried changing the figsize parameter but nothing changes the actual plot, only the image that contains it. Any ideas on how to change my plot so that the actual length of the axes are the same? Below is my plotting code:
def plot_plot(self, data, title="Pixel Plot"):
pixel_plot = plt.imshow(data)
plt.title(title)
plt.colorbar(pixel_plot)
plt.show(pixel_plot)
thanks in advance!
I think you want the aspect option in plt.imshow().
So something like plt.imshow(data, aspect=0.1) or plt.imshow(data, aspect='equal')
See this solution: https://stackoverflow.com/a/13390798/12133280

How to move plot to the left of window in matplotlib

I have a plot with a very wide legend.
I've managed to move the legend out of the plot so that it doesn't cover it, but the legend is too wide for the window and not completely visible. This could be corrected if I knew how to move plot and legend towards the left where there is spare space.
This is what I mean:
What instruction would allow me to do this?
My current code:
f,ax=plt.subplots(1)
f.set_size_inches(14,10.5)
...
plt.legend(bbox_to_anchor=(1,1), loc="upper left")
plt.show()
Thank you
You can try adjusting the margins. The line to do that is f.subplots_adjust(left=0.05, bottom=0.07, right=0.95, top=0.95, wspace=0, hspace=0). These values can be controlled from the button to the left of the save button on the bottom toolbar. So, you can try playing with those in the gui, then when you find a value you like or that works, enter them in the suggested line of code.
Then you code should look like .
f,ax=plt.subplots(1)
f.set_size_inches(14,10.5)
...
plt.legend(bbox_to_anchor=(1,1), loc="upper left")
f.subplots_adjust(left=0.05, bottom=0.07, right=0.95, top=0.95, wspace=0, hspace=0)
plt.show()

How to remove the whitespace between the tip of the arrow of a FancyArrowPatch and the target points?

When doing an image which puts lots of focus on some arrows, I noticed that the arrows didn't reach the full length between the specified points.
For example:
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots()
fig.set_size_inches(5, 5)
w = 0.001
h = 1000
ax.set_xlim(0, w)
ax.set_ylim(0, h)
ax.add_artist(mpl.patches.FancyArrowPatch(
(w*1/3, h*0.0),
(w*1/3, h*1.0),
edgecolor='black',
facecolor='red',
arrowstyle=mpl.patches.ArrowStyle.CurveFilledAB(head_length=10, head_width=10)
))
ax.add_artist(mpl.patches.FancyArrowPatch(
(w*2/3, h*0.0),
(w*2/3, h*1.0),
edgecolor='black',
facecolor='red',
arrowstyle=mpl.patches.ArrowStyle.Simple(head_length=10, head_width=10, tail_width=1)
))
plt.savefig(
'main.svg',
format='svg',
bbox_inches='tight',
dpi=100,
)
produces:
and here is a screenshot of a browser zoom of the arrows showing that they don't touch the black line above:
How to make them touch exactly without that white space?
Tested on matplotlib==3.2.2.
Related questions:
Alter the width of an annotate arrow in matplotlib
Matplotlib - set pad between arrow and text in annotate function
shrinkA=0 and shrinkB=0
This is the first main direct reason why arrows don't touch.
Those values default to 2, presumably because arrows were originally mostly used in annotations where some space is desired.
If I set them to zero:
FancyArrowPatch(
shrinkA=0,
shrinkB=0,
then the arrows touch as shown at:
A is for the start, and B is for the end of the arrow.
The effect of linewidth
Another thing to keep in mind however is that the linewidth can also affect if the arrow touches something or not.
For example, if I add an exaggerated linewidth=10 to the original code:
FancyArrowPatch(
shrinkA=0,
shrinkB=0,
linewidth=10,
then the result is:
so we note how:
the tracing of the edges is always rounded, and therefore the arrows stop being pointy and become rounded with large line widths
Simple and CurveFilledAB have different drawing algorithms: the one for Simple makes the arrow overrun the target, and CurveFilledAB makes it under run
the linewidth above was so fat that it hid the red inner color
For the case of Simple, if you don't need a different border color, you can get back the pointy arrow with linewidth=0, and control the main arrow line width with tail_width.
This does not work for CurveFilledAB however: the drawing algorithm for that style is to use the border alone for the main arrow line, so I'm not sure how to separately control the main line width of a double headed arrow without getting the rounded corners, except for drawing two simple arrows which is uglier: matplotlib simple and two head arrows Seems like something that could be patched by adding a new SimpleAB class.
The result of:
FancyArrowPatch(
shrinkA=0,
shrinkB=0,
linewidth=0,
is:

Are dotted/dashed colored vectors possible in matplotlib quiver?

I am working on a project and we decided to use matplotlib.
For a polar chart we have a bunch of colored vectors which need to be recognizable from each other.
Now I have been able to get this:
Quiver add is simply:
Q.append(sub.quiver(0,y_min_max[0], real_coords[i], imag_coords[i], color=color, scale=y_min_max[1]*2, zorder=5)
But is it possible to have dashed colored vectors?
The closest answer I found is this one Plotting dashed 2D vectors with matplotlib?
Which is close but I've only managed to get colored vectors surrounded with dashed lines instead of dashed vectors
Q.append(sub.quiver(0,y_min_max[0], real_coords[i], imag_coords[i], color=color, scale=y_min_max[1]*2, zorder=5, linewidth=0.5, linestyle = '--'))
I've been experimenting with various combinations but no luck for now, any ideas?
Thanks in advance

How to plot the whole point circle above axis line in matplotlib

Normally when you plot a list of points and axis. Only a part of point will be shown for those intersecting with the axis line, see the first point in this png for an example. How to make sure the whole point circle in shown above the axis line?
You can turn off the clipping by using the parameter clip_on of the plotting functions:
plt.plot(range(10), marker='o', ms=20, clip_on=False)
You can turn off clipping for the resulting plot artist(s) by setting clip_on=False in your call to plot or scatter. Note that you can also modify the clipping box by hand if you have a reference to the artist.
import matplotlib.pyplot as plt
plt.plot([0,1,2], [0,1,2], 'bo', clip_on=False)
produces: