Setting labels for image automatically - python

I have a 2D array, and I plot in into three lines in one image, each line represents the numbers in each column.
I would like to set their labels as 'column = 1', 'column = 2', 'column = 3' automatically, based on which column the line represent.
How can I achieve this?
#%%
import numpy as np
import matplotlib.pyplot as plt
data = [
[1, 2, 4],
[2, 4, 6],
[5, 7, 9]
]
arr = np.array(data)
#Plotting
arr = arr[:,0:3]
fig, ax = plt.subplots(figsize=(8, 5))
x = np.linspace(0, 2, 3)
x = np.asarray(x)
plt.plot(x, arr, label="column")
plt.legend(bbox_to_anchor=(0.85, 1), loc='upper left', borderaxespad=0.)
plt.show()

You can set the lables in the legend method,
...
lines = plt.plot(x, arr)
column_names = tuple("column_" + str(i+1) for i,x in enumerate(data))
plt.legend(lines, column_names, bbox_to_anchor=(0.85, 1), loc='upper left', borderaxespad=0.)
plt.show()

Related

How to modify the text arrangement in legend

I want to customize the number of rows in each column of the matplotlib legend text.
The code is:
import matplotlib.pyplot as plt
import numpy as np
for i in range(10):
plt.plot(np.linspace(0, 5, 5), np.random.randint(0, 10, 5), label=str(i))
plt.legend(ncol = 3, loc = 1)
plt.show()
What I get: the numbers of rows in each column are 4, 3, 3
What I want: the numbers of rows in each column are 4, 4, 2
I've tried adding plt.plot([], [], label='') before plt.show(), but it doesn't help
I solved this problem by adding an invisible line as 11th element like this:
import matplotlib.pyplot as plt
import numpy as np
lines = []
for i in range(10):
l, = plt.plot(np.linspace(0, 5, 5), np.random.randint(0, 10, 5), label=str(i))
lines.append(l)
l, = plt.plot(0, alpha=0, label=' ')
# alpha=0 will make the line and element in the legend invisible
lines.append(l)
plt.legend(handles=[l for l in lines], ncol = 3, loc = 1)
plt.show()
This will yield the following result:

Plotting time series data with with 30sec break point and color

I am new in python programming. I can simply plot the input data shown in the figure with my code but how can I plot the time series data as mention in the figure. Any code and suggestions will be thankful.
My code is:
import matplotlib.pyplot as plt
import numpy as np
y_values = [5, 5, 1, 1, 5, 5, 1, 1, 5, 1, 1]
x_values = np.arange(30, 331, 30)
plt.figure()
plt.plot(x_values,y_values,"-x")
plt.show()
Although there is a way to draw a series of rectangular shapes, we used a general method and used horizontal bar charts. We added a list for the values in the bar chart and stacked the values. Class label names and class titles are now supported as annotations. You can try various other parameters.
import matplotlib.pyplot as plt
import numpy as np
y = [5]*11
y_values = [5, 5, 1, 1, 5, 5, 1, 1, 5, 1, 1]
x_values = np.arange(30, 331, 30)
fig, ax = plt.subplots(figsize=(12,1))
ax.barh(y=0, height=1.0, edgecolor='k', width=y[0], label='Time Interval')
for i in range(len(y)):
if y_values[i] == 5:
color = 'y'
else:
color = 'm'
ax.barh(y=0, left=sum(y[:i]), height=1.0, width=y[i], color=color, edgecolor='k', label='Time Interval')
for s in ['top','bottom','left','right']:
ax.spines[s].set_visible(False)
for i,(p,t) in enumerate(zip(y, y_values)):
ax.text(y=0.6, x=2.5+p*i, s=str(t))
ax.text(-0.08, 1, 'Class', transform=ax.transAxes)
ax.set_xticks([])
ax.set_yticks([])
ax.set_ylabel('Time Interval', rotation=0, labelpad=40, loc='center')
plt.show()
Try:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
y_values = ['class', 5, 5, 1, 1, 5, 5, 1, 1, 5, 1, 1]
x_values = np.arange(30, 331, 30)
x_values = np.concatenate((['Time'],x_values))
df = pd.DataFrame(data={'class': y_values, 'Time': x_values})
colors = {5: 'gold', 1: 'darkviolet'}
df['colors'] = df['class'].map(colors)
df['colors'].fillna('white', inplace=True)
df['Time'].iloc[1:] = ''
print(df)
fig, ax =plt.subplots(1,1)
ax.axis('tight')
ax.axis('off')
data = df.T.values
colors = [data[2].tolist()]
table = ax.table(cellText=[data[1].tolist()], colLabels=data[0].tolist(),loc="center", cellColours=colors)
table.set_fontsize(14)
for i in range(len(data[0])):
table[0, i].visible_edges = ''
table[1, 0].visible_edges = ''
table.scale(1.5, 1.5)
plt.show()

Difference between graphs

def PlotPolly(model, independent_variable, dependent_variabble, Name):
x_new = np.linspace(15, 55, 100)
y_new = model(x_new)
plt.plot(independent_variable, dependent_variabble, '.', x_new, y_new, '-') #4
plt.title('Polynomial Fit with Matplotlib for Price ~ Length')
ax = plt.gca()
ax.set_facecolor((0.898, 0.898, 0.898))
fig = plt.gcf()
plt.xlabel(Name)
plt.ylabel('Price of Cars')
plt.show()
plt.close()
I get this with this code:
But when from line 4 I remove x_new and y_new line becomes
plt.plot(independent_variable, dependent_variabble)
I get this graph :
Can you explain what is meaning of x_new and y_new and why absence of this results in this kind of graph
In your code x_new and y_new both of them have the continuous values but independent_variable and dependent_variabble have ā€¨discontinuous values and for plot discontinues you need scatter plot. see this example:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([2, 1, 5, 3, 4, 2, 6, 4])
y = np.array([3, 1, 2, 0, 1, 2, 6, 4])
plt.plot(x, y, linestyle='-', marker='o')
Output:

Update text in matplotlib's plot

I want to change text in matplotlib's plot with loop. I am able to print text with loop, but unable to delete the previous text and they got printed on top of each other.
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5])
y = np.array([1,2,3,4,5])
fig, ax = plt.subplots()
ax.set_xlim([0,5])
ax.set_ylim([0,5])
for i in x:
pt = ax.plot(i, i, 'o')
tx = ax.text(1, 2, str(i), fontsize = 12)
plt.pause(1)
removePt = pt.pop()
removePt.remove()
I tried to delete text by
removeTx = tx.pop()
removeTx.remove()
but it has not worked.
Kindly suggest how can I remove the previous text from plot.
Just add tx.remove() after the pause:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 3, 4, 5])
y = np.array([1, 2, 3, 4, 5])
fig, ax = plt.subplots()
ax.set_xlim([0, 5])
ax.set_ylim([0, 5])
for i in x:
pt = ax.plot(i, i, 'o')
tx = ax.text(1, 2, str(i), fontsize = 12)
plt.pause(1)
tx.remove()
plt.show()

python plot multiple histograms

I have a dataframe X with 30 variables, v1, v2 ... v30 and
col_name=[v1,v2.....v30]
For each variable, I want to plot the histogram to understand the variable distribution. However, it is too manual to write code to plot one by one, can I have something like a for loop to draw 30 histograms one under another at one go?
For example:
for i in range(30):
hist(np.array(X[col_name[i]]).astype(np.float),bins=100,color='blue',label=col_name[i],normed=1,alpha=0.5)
How can I do that? Like one page of graphs (each with title and label) so that I can scroll down to read.
You could do something like this:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.normal(0, 10)
df = pd.DataFrame({
'v1': np.random.normal(0, 3, 20),
'v2': np.random.normal(0, 3, 20),
'v3': np.random.normal(0, 3, 20),
'v4': np.random.normal(0, 3, 20),
'v5': np.random.normal(0, 3, 20),
'v6': np.random.normal(0, 3, 20),
})
# Generically define how many plots along and across
ncols = 3
nrows = int(np.ceil(len(df.columns) / (1.0*ncols)))
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(10, 10))
# Lazy counter so we can remove unwated axes
counter = 0
for i in range(nrows):
for j in range(ncols):
ax = axes[i][j]
# Plot when we have data
if counter < len(df.columns):
ax.hist(df[df.columns[counter]], bins=10, color='blue', alpha=0.5, label='{}'.format(df.columns[counter]))
ax.set_xlabel('x')
ax.set_ylabel('PDF')
ax.set_ylim([0, 5])
leg = ax.legend(loc='upper left')
leg.draw_frame(False)
# Remove axis when we no longer have data
else:
ax.set_axis_off()
counter += 1
plt.show()
Results in:
Adapted from: How do I get multiple subplots in matplotlib?

Categories

Resources