Plot same plot twice with matplotlib? - matplotlib

I would like to plot the same mpl.pyplot.plot with different axes. My code looks like this:
import matplotlib.pyplot as plt
plt.subplot(211)
plot1 = plt.plot(data)
ax = plt.gca()
plt.axis('equal')
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
:
much more stuff
:
plt.subplot(212)
command_to_plot(last_plot)
ax.set_xlim(a, b)
But unfortunately command_to_plot does not exist. So how can I do this?

Related

How to plot the marker on top of the error bar in matplotlib?

I need to plot the following error bars for my project. And I control the size of the marker using matplotlib.scatter as follows:
import matplotlib.pyplot as plt
import numpy as np
x=[1,2,3]
y=[1,2,3]
yerr=[2,3,1]
fig,(ax1)=plt.subplots(1,1)
ax1.errorbar(x,y,yerr=yerr, linestyle='-', capsize=3, ecolor='lightblue', elinewidth=2)
ax1.scatter(x, y, s=[20]*len(x), marker='o', color='#1f77b4')
plt.show()
The results are like the following:
The markers are plotted under the error bar, which is not nice. Any solutions?
Try using zorder:
ax1.scatter(x, y, s=[20]*len(x), marker='o', color='#1f77b4', zorder=10)

Grid appears in TeX output after tikzplotlib save

I am using matplotlib with tikzplotlib to plot (PGF/TikZ) figures.
I do not want a grid to be plotted, however the grid is always activated in the TeX output.
How can I get around this?
I don't want to edit the .tex
if possible.
Bonus question: Is there also a way to get the legend title?
Example:
import numpy as np
import matplotlib.pyplot as plt
import tikzplotlib as tikz
x = np.arange(0, 5, 1)
y = x
fig, ax = plt.subplots()
ax.plot(x, y, label='asdf')
ax.grid(False)
ax.legend(title='asdf')
ax.set(xlabel='x',
ylabel='y')
plt.show()
tikz.clean_figure()
tikz.save("asdf.tex", figure=fig)
matplotlib plot (without grid)
tikzplotlib plot (grid activated)

I have been trying to embed my matplotlib graph into a pyqt5 application with a moving graph any help is appreciated

This code displays a moving graph with two lines, and the data is saved to a CSV file with the code that makes the data. I have tried to create a canvas class using the pyqt5 imports, but I am struggling with where exactly to put the matplotlib code.
from itertools import count
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.animation import FuncAnimation
plt.style.use('fivethirtyeight')
x_vals = []
y_vals = []
index = count()
def animate(i):
data = pd.read_csv("C:/Users/Khata/PycharmProjects/LiveData1/venv/data.csv")
x = data['x_value']
y1 = data['total_1']
y2 = data['total_2']
plt.cla()
plt.plot(x, y1, label='Channel 1')
plt.plot(x, y2, label='Channel 2')
plt.legend(loc='upper left')
plt.tight_layout()
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
plt.tight_layout()
plt.show()

matplotlib plt.show() doesn't show anything if axis is added after defining figure

I have a problem with plotting through matplotlib.
When I try to plot with this code works well:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1)
ax.plot(10)
plt.show()
But when I try this one, doesn't work:
import matplotlib.pyplot as plt
fig = plt.Figure()
ax = fig.add_subplot(111)
ax.plot(10)
plt.show()
Does someone know why this happens?
My matplotlib version is 3.3.4
Many thanks.
Take a closer look at the second line of your second code snippet, you're capitalizing the letter "f" in plt.Figure, it should be plt.figure instead. Bellow is the corrected version:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(10)
plt.show()
Your output will be:

Visualize 1-dimensional data in a sequential colormap

I have a pandas series containing numbers ranging between 0 and 100. I want to visualise it in a horizontal bar consisting of 3 main colours.
I have tried using seaborn but all I can get is a heatmap matrix. I have also tried the below code, which is producing what I need but not in the way I need it.
x = my_column.values
y = x
t = x
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.scatter(x, y, c=t, cmap='brg')
ax2.scatter(x, y, c=t, cmap='brg')
plt.show()
What I'm looking for is something similar to the below figure, how can I achieve that using matplotlib or seaborn?
The purpose of this is not quite clear, however, the following would produce an image like the one shown in the question:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
x = np.linspace(100,0,101)
fig, ax = plt.subplots(figsize=(6,1), constrained_layout=True)
cmap = LinearSegmentedColormap.from_list("", ["limegreen", "gold", "crimson"])
ax.imshow([x], cmap=cmap, aspect="auto",
extent=[x[0]-np.diff(x)[0]/2, x[-1]+np.diff(x)[0]/2,0,1])
ax.tick_params(axis="y", left=False, labelleft=False)
plt.show()