Two different plots from same loop in matplotlib?

2024/10/18 15:00:30

I would specifically like to create two different plots using one single loop. One plot should have four straight lines from x-y, and another plot should have four angled lines from x-y2. My code only shows everything in a single plot. I don't quite understand how plt works, how can I create two distinct plt objects?

import matplotlib.pyplot as plt
import matplotlib.pyplot as plt2x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]]
y2=[[11,12,13,24],[42,33,34,65],[23,54,65,86],[77,90,39,54]]
colours=['r','g','b','k']for i in range(len(x)):plt.plot(x[i],y2[i],colours[i])plt2.plot(x[i],y[i],colours[i])plt.show()
plt2.show()
Answer

Is that what you want to do?

import matplotlib.pyplot as pltx=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]]
y2=[[11,12,13,24],[42,33,34,65],[23,54,65,86],[77,90,39,54]]
colours=['r','g','b','k']fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
for i in range(len(x)):ax1.plot(x[i],y2[i],colours[i])ax2.plot(x[i],y[i],colours[i])fig1.show()
fig2.show()
https://en.xdnf.cn/q/72880.html

Related Q&A

Matplotlib text alignment

Is there a way to get the result shown in the third axes with just a single ax.text() command? Using expandtabs almost get me there, but the text never aligns properly. Using two plotting commands doe…

Pandas cannot load data, csv encoding mystery

I am trying to load a dataset into pandas and cannot get seem to get past step 1. I am new so please forgive if this is obvious, I have searched previous topics and not found an answer. The data is mos…

How to read in an edge list to make a scipy sparse matrix

I have a large file where each line has a pair of 8 character strings. Something like:ab1234gh iu9240ghon each line.This file really represents a graph and each string is a node id. I would like to r…

How can I find the best fuzzy string match?

Pythons new regex module supports fuzzy string matching. Sing praises aloud (now). Per the docs:The ENHANCEMATCH flag makes fuzzy matching attempt to improve the fitof the next match that it finds.The …

how to write a unicode csv in Python 2.7

I want to write data to files where a row from a CSV should look like this list (directly from the Python console):row = [\xef\xbb\xbft_11651497, http://kozbeszerzes.ceu.hu/entity/t/11651497.xml, "…

Terminating QThread gracefully on QDialog reject()

I have a QDialog which creates a QThread to do some work while keeping the UI responsive, based on the structure given here: How To Really, Truly Use QThreads; The Full Explanation. However, if reject(…

Python descriptors with old-style classes

I tried to google something about it. Why do non-data descriptors work with old-style classes?Docs say that they should not: "Note that descriptors are only invoked for new style objects or class…

Decrypting a file to a stream and reading the stream into pandas (hdf or stata)

Overview of what Im trying to do. I have encrypted versions of files that I need to read into pandas. For a couple of reasons it is much better to decrypt into a stream rather than a file, so thats m…

How to replace accents in a column of a pandas dataframe

I have a dataframe dataSwiss which contains the information Swiss municipalities. I want to replace the letter with accents with normal letter.This is what I am doing:dataSwiss[Municipality] = dataSwis…

Comparison of multi-threading models in Julia =1.3 and Python 3.x

I would like to understand, from the user point of view, the differences in multithreading programming models between Julia >= 1.3 and Python 3.Is there one that is more efficient than the other (in…