#๐Ÿ”’ Matplotlib help

7 messages ยท Page 1 of 1 (latest)

visual idol
#

How do I create a heat map or a change in color along an axis on a scatter plot with matplotlib.pyplot.

Context:

Could I get some help with matplotlib.pyplot please. I have a csv file with two different columns, data 1 and 2 with 100 sets of data. I separated the columns into lists and scattered the list with plt.scatter. But need assistance with creating a heat map or change of color along an axis.

fierce windBOT
#

@visual idol

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

visual idol
#

My code:

import matplotlib.pyplot as plt
import sys

Create two empty lists

column1 = []
column2 = []

Opening first command line argument

with open(sys.argv[1], 'r') as file:

# Skips the header
header = file.readline()

# Reads data
lines = file.readlines()

## Cleans data and sets data as float
for line in lines:
    absyss_data = line.strip().split(',')

    if len(absyss_data) >= 2:

        # Cleans percent signs in first and second row of data
        value1 =  float(absyss_data[0].replace('%', '').strip())
        value2 =  float(absyss_data[1].replace('%', '').strip())

        # Appends new data
        column1.append(value1)
        column2.append(value2)

Scatter plot two lists

Scatter the two lists

plt.scatter(column1, column2)

Create title

plt.title("Student Overall Grading")

Label axes

plt.xlabel('Overall grade')
plt.ylabel('Grade for comments in lab')

plt.show()

Save graph as command line argument 2

plt.savefig(sys.argv[2])

peak flower
# visual idol How do I create a heat map or a change in color along an axis on a scatter plot ...

in pyplot you do this using .imshow() and something like cmap="hot" interpolation='nearest':
https://stackoverflow.com/questions/33282368/plotting-a-2d-heatmap
with extra annotations:
https://matplotlib.org/stable/gallery/images_contours_and_fields/image_annotated_heatmap.html
or you can use seaborn's heatmap
https://seaborn.pydata.org/generated/seaborn.heatmap.html

visual idol
#

I used the .imshow() function however itโ€™s not displaying color

fierce windBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.