matplotlib.pyplot: add horizontal line to sub-plot [duplicate] - python

This question already has answers here:
Plot a horizontal line on a given plot
(7 answers)
Closed 4 years ago.
This code (matplotlib.pyplot) gives me the plot in the link below:
plt.subplot(2, 1, 1)
plt.plot(px,py)
plt.subplot(2, 1, 2)
plt.plot(curve)
2 plots example
--> I want to add a horizontal line in the second sub-plot at 100.000. How can I do that? The colors of both plots shall stay the same/synchronized.

You can use matplotlib.axes.Axes.axhline of matplotlib which adds a horizontal line across the axis. If you need to set any further parameters, refer to the official documentation
import matplotlib.pyplot as plt
plt.axhline(100000, color="gray")

Related

setting legend values according to categorical data in matplotlib [duplicate]

This question already has answers here:
Matplotlib scatter plot legend
(5 answers)
Matplotlib - Adding legend to scatter plot [duplicate]
(1 answer)
Matplotlib scatter plot with legend
(6 answers)
Closed last month.
I'm trying to set the legend according to categorical values set for color but it's not working.
import matplotlib.pyplot as plt
colors = finalDf['sales'].astype('category').cat.codes
scatter = plt.scatter(x= finalDf['principal component 1'],
y= finalDf['principal component 2'],
c = finalDf['sales'].astype('category').cat.codes)
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.legend(labels=list(finalDf['sales'].unique()))
plt.show()
(https://i.stack.imgur.com/YwSoG.png)
I tried various combinations of the 'sales' data columns but it was in vain. I want to know if there is a solution other than using seaborn library as the task recquires matplotlib. Thank you

Emphasize on line of the grid with Seaborn [duplicate]

This question already has answers here:
Plot a horizontal line on a given plot
(7 answers)
Closed 1 year ago.
I am using Seaborn to plot a violin graph (as you can see on the pic below):
I would like to emphasize the line at the 0 level on the y-axis (Comparison_perc) and then make it slightly bigger/darker.
Would you know how?
Many thanks,
Guillaume
I believe you can add a horizontal line using axhline() e.g.
#Import libraries
import seaborn as sns
import matplotlib.pyplot as plt
#Load example dataset
dataset = sns.load_dataset("iris")
#Create graph
graph = sns.barplot(x="sepal_width", y="petal_width", data=dataset)
#Draw a horizontal line at 0
graph.axhline(y=0)
#Show plot
plt.show()
And you can change the color/thickness etc, e.g. graph.axhline(y=0, linewidth=4, color='r')

How to plot y-axis to the opposite side? [duplicate]

This question already has answers here:
matplotlib y-axis label on right side
(4 answers)
Closed 2 years ago.
I have this chart below:
I would want the y-axis for the lower subplot to be plotted to the opposite side since that would make more sense. Is there a method for this? The ax.invert_yaxis() simply inverts the labels.
Note: For the curious, I simply used .invert_xaxis() to plot inverted bars.
I guess, what you are looking for is
ax[1].yaxis.set_ticks_position("right")
ax[1].yaxis.set_label_position("right")
of an axis object.
So with #meTchaikovsky's MVE code, you'll get
import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(1,10,10)
y0 = np.random.randint(0,30,size=10)
fig,ax = plt.subplots(nrows=2,ncols=1,figsize=(8,6))
ax[1].set_xlim(0,30)
ax[0].barh(x,y0,color='violet')
ax[0].set_ylabel("Y-Axis")
ax[1].set_xlim(30,0)
ax[1].barh(x,y0,color='deepskyblue')
ax[1].yaxis.set_ticks_position("right")
ax[1].yaxis.set_label_position("right")
ax[1].set_ylabel("Y-Axis")
plt.show()

How can you create a line graph that the same line has two different colors in matplotlib? [duplicate]

This question already has answers here:
Can I make a multi-color line in matplotlib?
(3 answers)
How to plot one line in different colors
(5 answers)
Closed 3 years ago.
I have a regular line graph
plt.figure(figsize=(10,5))
plt.plot(db['month_date'], db['quantity'], alpha=0.7)
plt.ylabel('quantity')
plt.title('Quantity cards ')
plt.show()
This generates a normal line graph with a blue line, is there a way that the last three values of quantity are highlithed in a different color? So the normal Blue line and the last three values being red perhaps. If any one can help I would appreciate it very much.
I'm not sure what you mean by last three values, last three values based on the x-axis? Or the y-axis. Anyway, have a look at this.
Example of multi-colored lines in Matplotlib documentation

Show histogram bar outline [duplicate]

This question already has answers here:
No outlines on bins of Matplotlib histograms or Seaborn distplots
(3 answers)
Closed 5 years ago.
df3['a'].plot.hist(color='blue',xlim=(0,1))
I want to know how can it show the line in the histogram figure.
Make the top figure showed as bottom figure. Thank you!
Pass the edgecolor argument to hist.
df3['a'].plot.hist(color='blue',
edgecolor='black',
xlim=(0,1))
Demo
df = pd.DataFrame(dict(vals=np.random.normal(size=100)))
df.plot.hist(edgecolor='black')

Categories