I am getting some sort of underlaying line when using dashed lines in pyplot - numpy

This is my code for one of my lab problems in my coding class:
t1=np.linspace(-30,-1.6,100)
x1=(3*t1)/(1+t1**3)
y1=(3*t1**2)/(1+t1**3)
t2=np.linspace(-0.6,40,1000)
x2=(3*t2)/(1+t2**3)
y2=(3*t2**2)/(1+t2**3)
plt.subplot(111)
plt.plot(x1,y1)
plt.plot(x2,y2)
plt.plot(x1,y1,color="blue", linestyle="-")
plt.plot(x2,y2,color="green", linestyle="--", dashes=(5,5))
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.xlim(-2.5,2)
plt.ylim(-2.5,2)
plt.title("p06")
plt.show()
https://imgur.com/a/jh6Ta
My program is for some reason imputing this orange line underneath my green dashed line. Does anyone know why this is occurring?

Related

How can I exclude overlapping shapes to make a dashed line always visible against a solid overlap?

I am trying to recreate the dashed line effect seen in the attached image using matplotlib.
I could not find any reference
To do this you need Matplotlib v3.6.0 or higher, in which the (currently experimental) gapcolor option was added for dashed lines. You can see an example here, and I'll show an example below:
from matplotlib import pyplot as plt
import numpy as np
d = np.random.randn(30)
plt.plot(d, "g", lw=10)
# overplot a dashed line with gapcolor set to white
# (in your example image the linestyle is ":" rather than "--", so choose what you like)
plt.axhline(0, color="g", ls="--", gapcolor="white")
This gives:
If you don't have the latest Matplotlib version, or can't upgrade, you can get the effect by just plotting a white line and then a dashed line over the top (or vice versa), e.g.,
plt.plot(d, "g", lw=10)
# plot a white line
plt.axhline(0, color="white")
# overplot a dashed green line
plt.axhline(0, color="g", ls="--"")

Aliasing in heatmap gridlines

I am following the tutorial on matplotlib for creating heatmaps: https://matplotlib.org/stable/gallery/images_contours_and_fields/image_annotated_heatmap.html#a-simple-categorical-heatmap
I run the code with a slight modification, which is changing the following line:
ax.grid(which="minor", color="w", linestyle='-', linewidth=3)
into
ax.grid(which="minor", color="black", linestyle='-', linewidth=1)
I get the following plot:
There is a small distortion in the line separating the second row with the third row. The line is not exactly between the rows of cells.
Zooming in on this distortion we have:
I believe this is due to aliasing? Is there a simple fix for this?

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()

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

highlight single contour line

I'm making a simple contour plot and I want to highlight the zero line by making it thicker and changing the color.
cs = ax1.contour(x,y,obscc)
ax1.clabel(cs,inline=1,fontsize=8,fmt='%3.1f')
How do I achieve this?
Thanks :-)
HTH -- this is basically the contour example taken from the matplotlib docs, just with modified level lines
The object that is returned from the contour-method holds a reference to the contour lines in its collections attribute.
The contour lines are just common LineCollections.
In the following code snippet the reference to the contour plot is in CS (that is cs in your question):
CS.collections[0].set_linewidth(4) # the dark blue line
CS.collections[2].set_linewidth(5) # the cyan line, zero level
CS.collections[2].set_linestyle('dashed')
CS.collections[3].set_linewidth(7) # the red line
CS.collections[3].set_color('red')
CS.collections[3].set_linestyle('dotted')
type(CS.collections[0])
# matplotlib.collections.LineCollection
Here's how to find out about the levels, if you didn't explicitly specify them:
CS.levels
array([-1. , -0.5, 0. , 0.5, 1. , 1.5])
There is also a lot of functionality to format individual labels:
CS.labelCValueList CS.labelIndiceList CS.labelTextsList
CS.labelCValues CS.labelLevelList CS.labelXYs
CS.labelFmt CS.labelManual CS.labels
CS.labelFontProps CS.labelMappable CS.layers
CS.labelFontSizeList CS.labelTexts