首页 后端开发 Python教程 免费项目:使用 ChatGPT 通过 Python 和 Matplotlib 进行绘图

免费项目:使用 ChatGPT 通过 Python 和 Matplotlib 进行绘图

Aug 31, 2024 am 06:02 AM

Free Project: Use ChatGPT to Plot with Python and Matplotlib

想学习用 Python 绘图吗? ChatGPT 将为您提供帮助。

该项目要求 ChatGPT 教您如何生成用于创建绘图和可视化的 Python 代码。只需提供您想要创建的绘图的简短描述,ChatGPT 将使用流行的 Matplotlib 库生成必要的代码。该工具甚至允许没有编程经验的用户为其数据开发专业级绘图。无论您是研究人员、数据科学家还是学生,ChatGPT 都可以让您轻松地用 Python 创建美观且信息丰富的绘图。 (由 ChatGPT 生成,稍作修改)。

JupyterNotebook 中的项目可以在认知课程中免费使用。


1. 让我们询问一下我们想要学习的具体兴趣领域。

询问:“教我如何用 python 绘图”
结果:
在 Python 中绘制数据的方法有很多种。这是一个使用 Matplotlib(Python 中流行的数据可视化库)的简单示例:

# using the provided by chatGPT to run in the notebook
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
# Create a figure and an axes
fig, ax = plt.subplots()
# Plot the data
ax.plot(x, y)
# Add a title
ax.set_title('My Plot')
# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

这将创建 x 和 y 数据的简单线图。有许多选项和自定义选项可用于在 Matplotlib 中创建绘图。例如,您可以更改线条的外观、更改轴标签、添加图例等。

以下是有关 Matplotlib 和 Python 中其他绘图库的更多信息:
Matplotlib 文档:https://matplotlib.org/stable/index.html
Matplotlib 教程:https://matplotlib.org/stable/tutorials/index.html
Seaborn,一个基于Matplotlib的数据可视化库:https://seaborn.pydata.org/
Plotly,用于创建交互式绘图的库:https://plotly.com/python/

如您所见,ChatGPT 提供了介绍性文本和代码块(为了方便起见,带有复制图标),并通过其他有用信息结束对话。

2.您可以提出后续问题

只需询问“向我展示更多Python 绘图示例”。事实上,chatGPT 会记住您之前的对话。
结果:
以下是您可以使用 Matplotlib 库在 Python 中创建的更多绘图示例:散点图

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the data
ax.scatter(x, y)

# Add a title
ax.set_title('My Scatter Plot')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

条形图

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the data
ax.bar(x, y)

# Add a title
ax.set_title('My Bar Plot')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

饼图

import matplotlib.pyplot as plt

# Sample data
labels = ['Apples', 'Bananas', 'Oranges']
sizes = [30, 45, 25]

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the data
ax.pie(sizes, labels=labels)

# Add a title
ax.set_title('My Pie Chart')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

请记住,这些只是几个示例,您可以使用 Matplotlib 在 Python 中创建许多其他类型的绘图。您可以在 Matplotlib 文档中找到更多信息和示例:https://matplotlib.org/stable/index.html。

让我们根据示例数据集进行绘图

3.建议一些示例数据集

初始阶段需要加载数据。我们可以通过询问“你能告诉我Python初学者的示例数据集吗?”
来要求ChatGPT推荐一些示例数据集。 结果:
当然!以下是一些可能对 Python 初学者有用的数据集:

鸢尾花数据集:这是一个小型数据集,包含不同品种鸢尾花的信息,包括萼片和花瓣的长度和宽度。它通常用作初学者学习机器学习和数据分析技术的数据集。您可以使用 sklearn.datasets 模块在 Python 中加载 Iris 数据集。

波士顿住房数据集:该数据集包含波士顿地区的房价信息,包括房屋中位值、犯罪率等因素。它通常用作初学者学习回归技术的数据集。您可以使用 sklearn.datasets 模块在 Python 中加载 Boston Housing 数据集。

MNIST数据集:这是一个手写数字的数据集,有60,000个训练样本和10,000个测试样本。它通常用作初学者学习图像分类技术的数据集。您可以使用 keras.datasets 模块在 Python 中加载 MNIST 数据集。

From these suggested datasets, we select Iris dataset to continue to work with as a sample.

3.1. Load the iris dataset as a sample to work wit

h

We need a proper sample in tabular format, so we asked “load iris data with table looking format”
The result:

To load the Iris dataset in a table-like format in Python, you can use the Pandas library. Here is an example of how you can load the Iris dataset and display it in a table-like format:

import pandas as pd
from sklearn import datasets

# Load the Iris dataset
iris = datasets.load_iris()

# Create a DataFrame with the features and target values
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['target'] = iris.target

# Display the DataFrame
print(df)
#--> or we can call the dataset to displace it
df

Free Project: Use ChatGPT to Plot with Python and Matplotlib

_This will create a Pandas DataFrame with the features (sepal length, sepal width, petal length, and petal width) of the Iris dataset as columns, and the target values (the species of iris) as an additional column.

You can then use the usual Pandas functions to manipulate and analyze the data in the DataFrame. For example, you can use the head() function to display the first few rows of the DataFrame, or use the describe() function to get summary statistics for the numerical columns._

4. Plot the dataset

Conveniently, we can ask follow-up questions; here we asked “plot it”
The result:

To plot the Iris dataset using Matplotlib, you can use the following code:

# --> Trying of the suggestions from ChatGPT
# --> we removed the redundant lines (e.g. loading libraries and importing data) from the suggestion

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the data, coloring the points by the target values
scatter = ax.scatter(df['sepal length (cm)'], df['sepal width (cm)'], c=df['target'])

# Add a legend
legend = ax.legend(*scatter.legend_elements(), title='Species')

# Add a title
ax.set_title('Iris dataset')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

4.1. Plot-specific column

We select and ask to plot a specific column in the dataset. For example, asking “show me the distribution of the column ‘sepal length (cm)”
The result:

To show the distribution of a column in a Pandas DataFrame, you can use the hist() function of the DataFrame. For example, to show the distribution of the ‘sepal length (cm)’ column in the Iris dataset, you can use the following code:

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the distribution of the 'sepal length (cm)' column
df['sepal length (cm)'].hist(ax=ax)

# Add a title
ax.set_title('Distribution of sepal length')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

As you may notice, the plot does not have labels. Therefore, we can **ask “redo it with proper axis labels”.
The result:
**

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the distribution of the 'sepal length (cm)' column
df['sepal length (cm)'].hist(ax=ax)

# Add axis labels
ax.set_xlabel('Sepal length (cm)')
ax.set_ylabel('Frequency')

# Add a title
ax.set_title('Distribution of sepal length')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

4.2. More complex plot

Another useful plot to show the distribution is using boxplot which enables us to visualize standard deviation and identify outliers.

Here we ask for a boxplot of all the columns showing in the single plot by asking “show me boxplot of distributions of all columns in same plot”.
The result:

To show the distribution of all columns in a Pandas DataFrame as boxplots on the same plot, you can use the boxplot() function of the DataFrame.

For example, to show the distributions of all columns in the Iris dataset as boxplots, you can use the following code:

import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets

# Load the Iris dataset
iris = datasets.load_iris()

# Create a DataFrame with the features
df = pd.DataFrame(iris.data, columns=iris.feature_names)

# Add a column with the target values
df['target'] = iris.target

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the boxplots of all columns
df.boxplot(ax=ax)

# Add a title
ax.set_title('Distributions of all columns')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

5. Using chatGPT for your own project

ChatGPT is an excellent tool for working on your project. It is important to frame the question in a way that addresses your problem. Keep in mind that dividing your project into smaller blocks and addressing them individually is the key to addressing the project.

To ask a programming question from ChatGPT, you can simply provide a clear and concise description of your problem, along with any relevant code or error messages. You should also specify the programming language and any libraries or frameworks that you are using.

5.1. How to properly formulate a question (aka. prompt) for ChatGPT

So, let's ask ChatGPT what it thinks.

When formulating a prompt for ChatGPT, it’s important to keep a few things in mind in order to get the most out of the model:
Be specific and clear about what you want the model to do. For example, instead of asking “What do you know about this topic?”, ask “Can you provide a brief overview of the history of the topic?”

Provide context if necessary. If you’re asking a question about a specific event or person, give some background information so that the model can understand the context in which you’re asking the question.

要简洁。冗长而复杂的提示可能会让模型难以理解,并可能导致响应不太准确或相关。

避免使用模型可能不熟悉的过于技术性的语言或术语。

避免在一个提示中提出多个问题,最好一次问一个问题。

以下是 ChatGPT 的一些良好提示示例:
“你能解释一下监督学习和无监督学习之间的主要区别吗?”

“神经网络的主要组成部分是什么以及它们如何工作?”

“请写一个关于能读心术的机器人的小故事”

另一方面,一些不良提示的示例是:

“一切”
“你能告诉我什么?”
“你知道什么”

通过遵循这些指南,您可以帮助确保在使用 ChatGPT 时从模型中获得最准确且相关的响应。

5.2.作为数据科学家请记住

ChatGPT 并不完美;提供的示例来自 sklearn。在现实世界中,您需要使用 Pandas 从您的资源加载自己的数据(下面的示例显示从本地存储加载 iris)。

一般来说,作为一名实践数据科学家,您会经常遇到数据收集、准备、模型构建和解释的多方面过程。这些阶段在技能和资源方面都有自己独特的要求。例如,您可以使用 pandas 来加载和操作数据集。请务必记住,ChatGPT 提供的示例旨在作为基础学习辅助工具,可能不适合处理更复杂或大量的数据集。

5.3.不要惊慌,GPT 不会取代数据科学家(目前为止!)

那么,让我们问问 ChatGPT 它是怎么想的。

ChatGPT 和其他大型语言模型不会取代数据科学家有几个原因:

数据科学家拥有更广泛的技能:数据科学家接受过广泛技能的培训,包括数据分析、机器学习、编程、统计等。 ChatGPT 和其他大型语言模型只能根据给定的提示生成文本,并且不具备数据科学家所拥有的其他技能。

数据科学家可以解释和解释他们的工作结果:数据科学家接受过解释和解释他们的工作结果的培训,这对于决策和与利益相关者的沟通至关重要。 ChatGPT 和其他大型语言模型无法解释或解释其工作结果。

数据科学家可以处理各种数据:数据科学家接受过处理各种数据(包括结构化和非结构化数据)的培训,他们可以使用各种工具和技术从数据中提取见解。 ChatGPT 和其他大型语言模型只能根据给定的提示生成文本,并且无法处理其他类型的数据。

数据科学家可以与广泛的利益相关者合作:数据科学家接受过与广泛的利益相关者合作的培训,包括商业领袖、主题专家和技术专家,他们可以以清晰易懂的方式传达复杂的概念方式。 ChatGPT 和其他大型语言模型无法与利益相关者合作或传达复杂的概念。

查看这个适合初学者的指导项目以及由 IBM 开发人员和数据科学家开发的其他免费课程,网址为 https://cognitiveclass.ai/

下次再见!

以上是免费项目:使用 ChatGPT 通过 Python 和 Matplotlib 进行绘图的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Laravel 教程
1605
29
PHP教程
1510
276
成品python大片在线观看入口 python免费成品网站大全 成品python大片在线观看入口 python免费成品网站大全 Jul 23, 2025 pm 12:36 PM

本文为您精选了多个顶级的Python“成品”项目网站与高水平“大片”级学习资源入口。无论您是想寻找开发灵感、观摩学习大师级的源代码,还是系统性地提升实战能力,这些平台都是不容错过的宝库,能帮助您快速成长为Python高手。

用于量子机学习的Python 用于量子机学习的Python Jul 21, 2025 am 02:48 AM

要入门量子机器学习(QML),首选工具是Python,需安装PennyLane、Qiskit、TensorFlowQuantum或PyTorchQuantum等库;接着通过运行示例熟悉流程,如使用PennyLane构建量子神经网络;然后按照数据集准备、数据编码、构建参数化量子线路、经典优化器训练等步骤实现模型;实战中应避免一开始就追求复杂模型,关注硬件限制,采用混合模型结构,并持续参考最新文献和官方文档以跟进发展。

python run shell命令示例 python run shell命令示例 Jul 26, 2025 am 07:50 AM

使用subprocess.run()可安全执行shell命令并捕获输出,推荐以列表传参避免注入风险;2.需要shell特性时可设shell=True,但需警惕命令注入;3.使用subprocess.Popen可实现实时输出处理;4.设置check=True可在命令失败时抛出异常;5.简单场景可直接链式调用获取输出;日常应优先使用subprocess.run(),避免使用os.system()或已弃用模块,以上方法覆盖了Python中执行shell命令的核心用法。

从Python中的Web API访问数据 从Python中的Web API访问数据 Jul 16, 2025 am 04:52 AM

使用Python调用WebAPI获取数据的关键在于掌握基本流程和常用工具。1.使用requests发起HTTP请求是最直接的方式,通过get方法获取响应并用json()解析数据;2.对于需要认证的API,可通过headers添加token或key;3.需检查响应状态码,推荐使用response.raise_for_status()自动处理异常;4.面对分页接口,可通过循环依次请求不同页面并加入延时避免频率限制;5.处理返回的JSON数据时需根据结构提取信息,复杂数据可用pandas转换为Data

python seaborn关节图示例 python seaborn关节图示例 Jul 26, 2025 am 08:11 AM

使用Seaborn的jointplot可快速可视化两个变量间的关系及各自分布;2.基础散点图通过sns.jointplot(data=tips,x="total_bill",y="tip",kind="scatter")实现,中心为散点图,上下和右侧显示直方图;3.添加回归线和密度信息可用kind="reg",并结合marginal_kws设置边缘图样式;4.数据量大时推荐kind="hex",用

如何加入Python的字符串列表 如何加入Python的字符串列表 Jul 18, 2025 am 02:15 AM

在Python中,使用join()方法合并字符串需注意以下要点:1.使用str.join()方法,调用时前面的字符串作为连接符,括号里的可迭代对象包含要连接的字符串;2.确保列表中的元素都是字符串,若含非字符串类型需先转换;3.处理嵌套列表时需先展平结构再连接。

python列表到字符串转换示例 python列表到字符串转换示例 Jul 26, 2025 am 08:00 AM

字符串列表可用join()方法合并,如''.join(words)得到"HelloworldfromPython";2.数字列表需先用map(str,numbers)或[str(x)forxinnumbers]转为字符串后才能join;3.任意类型列表可直接用str()转换为带括号和引号的字符串,适用于调试;4.自定义格式可用生成器表达式结合join()实现,如'|'.join(f"[{item}]"foriteminitems)输出"[a]|[

Python网络刮擦教程 Python网络刮擦教程 Jul 21, 2025 am 02:39 AM

掌握Python网络爬虫需抓住三个核心步骤:1.使用requests发起请求,通过get方法获取网页内容,注意设置headers、处理异常及遵守robots.txt;2.利用BeautifulSoup或XPath提取数据,前者适合简单解析,后者更灵活适用于复杂结构;3.针对动态加载内容使用Selenium模拟浏览器操作,虽速度较慢但能应对复杂页面,也可尝试寻找网站API接口提高效率。

See all articles