Matplotlib plt.plot with enumerate not working

2024/10/12 7:17:45
import numpy as np 
import matplotlib.pyplot as plt array = np.array([[1,2,3,4,5,6],[10,20,30,40,50,60],[3,4,5,6,7,8],[100,200,300,400,500,600]])def plot(list):fig = plt.figure()ax = fig.add_subplot(111)for a,i in enumerate(list.T):ax.scatter(i[0],i[1],c='red') # This is plottedax.plot(i[2],i[3],'g--') # THIS IS NOT BEING PLOTTED !!!! fig.show()plot(array)

Now, I need to call plot several times using different array lists. So my for loop cannot be removed. Is there any other way to plot a dotted line apart from calling plt.plot instead ?

This is the plot I get:

enter image description here

As you can see, I am not getting the plt.plot(i[2],i[3],'g--'). Why is this so ?

But when you print the values using the same for loop:

In [21]: for a,i in enumerate(array.T):...:     print i[2],i[3]...:     
3 100
4 200
5 300
6 400
7 500
8 600

The values are perfectly printed. They however are not plotted.

Answer

Remove the for loop:

ax.scatter(array[0],array[1],c='red')
ax.plot(array[0],array[1],'g--')

The problem with your code is that you iterate over rows, which is fine for plotting single dots (ax.scatter), but not for connecting single dots (ax.plot with '--' option): at each row you only plot the line between that point and itself, which obviously doesn't show up in the graph.

https://en.xdnf.cn/q/118224.html

Related Q&A

using complex conditions to form a pandas data frame from the existing one

Ive got the following dataframe containing function names, their arguments, the default values of the arguments and argument types:FULL_NAME ARGUMENT DEF_VALS TYPE function1 f1_arg1 NAN …

Crawl and scrape a complete site with scrapy

import scrapy from scrapy import Request#scrapy crawl jobs9 -o jobs9.csv -t csv class JobsSpider(scrapy.Spider): name = "jobs9" allowed_domains = ["vapedonia.com"] start_urls = [&qu…

Why is pip freezing and not showing a module, although pip install says its already installed?

Im following these instructions to install Odoo on Mac. It required that I install all the Python modules for the user like so: sudo pip install -—user -r requirements.txt(*A note about the --user par…

Flatten a list of strings which contains sublists

I have a list of strings which contains a sublist os strings:ids = [uspotify:track:3ftnDaaL02tMeOZBunIwls, uspotify:track:4CKjTXDDWIrS0cwSA9scgk, [uspotify:track:6oRbm1KOqskLTFc1rvGi5F, uspotify:track:…

Portscanner producing possible error

I have written a simple portscanner in python. I have already asked something about it, you can find the code here.I corrected the code and now am able to create a connection to e.g. stackoverflow.netB…

Import error on first-party library with dev_appserver.py

On Ubuntu 16.04, am suddenly getting import errors from the local GAE development server. The local dev server starts up, including the admin interface, but app no longer loads.Native python imports o…

Split dictionary based on values

I have a dictionary:data = {cluster: A, node: B, mount: [C, D, E]}Im trying to split the dictionary data into number of dictionaries based on values in key mount.I tried using:for value in data.items()…

Using defaultdict to parse multi delimiter file

I need to parse a file which has contents that look like this:20 31022550 G 1396 =:0:0.00:0.00:0.00:0:0:0.00:0.00:0.00:0:0.00:0.00:0.00 A:2:60.00:33.00:37.00:2:0:0.02:0.02:40.00:2:0.98:126.00…

Iterating in DataFrame and writing down the index of the values where a condition is met

I have a data made of 20 rows and 2500 columns. Each column is a unique product and rows are time series, results of measurements. Therefore each product is measured 20 times and there are 2500 product…

Access denied to ClearDB database using Python/Django on Heroku

Im trying to build a webapp on Heroku using Python/Django, and I just followed the tutorial to set up a Django project and push it to Heroku. However, I can never even get to the normal Django "I…