【数据分析】绘制统计图

折线图

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y_1 = np.sin(x)
y_2 = np.cos(x)
y_3 = np.tanh(x)
plt.figure(figsize=(12,4))
plt.plot(x, y_1, 'r-o',label="$sin(x)$", linewidth = 0.5)
plt.plot(x, y_2, 'b-*',label="$cos(x)$", linewidth = 0.5)
plt.plot(x, y_3, 'g-^',label="$tanh(x)$", linewidth = 0.5)
plt.xlabel('Time(s)')
plt.ylabel("Volt")
plt.title("Python chart")
plt.legend() # 显示标签
plt.show()
plt.savefig("name.jpg")

在这里插入图片描述

频率分布直方图

# 运行以下代码
# sort the values from the top to the least value and slice the first 5 items
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

path7 = './data.csv'  # train.csv
titanic = pd.read_csv(path7)
print(titanic.describe())  #输出平均值,方差之类的基本统计信息

df = titanic['data'].sort_values(ascending = False)

# create bins interval using numpy
binsVal = np.arange(0,100000,4000) #边界自己设置,从0~100000,以4000为等差,进行增加

# create the plot
plt.hist(df, bins = binsVal)

# Set the title and labels
plt.xlabel('data')      #设置x标签的名字
plt.ylabel('Frequency') #设置y标签的名字
plt.title('Fare Payed Histrogram')

# show the plot
plt.show()

这里写图片描述

正态分布直方图

# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import os


x = []
csvFile1 = open('G:/Test/5-25/data.csv','r',newline='')
with csvFile1 as f:
    for line in f.readlines():
        x.append(int(line.split(',')[0]))    

mu = np.mean(x)
sigma = np.std(x)
# x = mu + sigma * np.random.randn(10000)
# 在均值周围产生符合正态分布的x值

num_bins = 50  #50个柱形

n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
# 直方图函数: x为x轴的值,normed=1表示为概率密度,即和为一,绿色方块,色深参数0.5.
# 返回值为n个概率,直方块左边线的x值,及各个方块对象

y = mlab.normpdf(bins, mu, sigma)
# 画一条逼近的曲线,y只负责绘制一条高斯分布的曲线

plt.plot(bins, y, 'r--')
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'Histogram of IQ: $\mu=100$ $\sigma=15$') # 中文标题 u'xxx'

plt.subplots_adjust(left=0.15) # 左边距
plt.show()

这里写图片描述

饼图

# 运行以下代码
# sort the values from the top to the least value and slice the first 5 items
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

path7 = './data.csv'  # train.csv
titanic = pd.read_csv(path7)
## 运行以下代码
# sum the instances of males and females
class1 = (titanic['class'] == 1).sum()
class2 = (titanic['class'] == 2).sum()
class3 = (titanic['class'] == 3).sum()
class4 = (titanic['class'] == 4).sum()

# put them into a list called proportions
proportions = [class1,class2,class3,class4]

# Create a pie chart
plt.pie(
    # using proportions
    proportions,
    
    # with the labels being officer names
    labels = ['class1', 'class2','class3','class4'],  #标签
    
    # with no shadows
    shadow = False,
    
    # with colors
    colors = ['blue','red','green','yellow'], #标签颜色
    
    # with one slide exploded out
    explode = (0.15 , 0 , 0 , 0 ), #离心距离,在此只有蓝色,也就是第一个离心了
    
    # with the start angle at 90%
    startangle = 90,
    
    # with the percent listed as a fraction
    autopct = '%1.1f%%'#保留小数点之后1位
    )

# View the plot drop above
plt.axis('equal')

# Set labels
plt.title("Sex Proportion")

# View the plot
plt.tight_layout()
plt.show()

这里写图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在Java中读取数据并绘制统计图,你可以使用以下步骤: 1. 读取数据:使用Java中的文件读取器或数据库连接器来读取数据。你需要将数据存储在一个数组或集合中。 2. 统计数据:对数据进行统计分析,例如计算平均值、中位数、众数等。 3. 绘制统计图:使用Java图形库(例如JFreeChart)来绘制统计图。你需要选择适当的图表类型,例如柱状图、折线图、饼图等,并提供必要的数据、标签和标题。 下面是一个简单的示例程序,演示如何读取数据并绘制柱状图: ```java import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; public class StatisticsDemo { public static void main(String[] args) { // 读取数据 ArrayList<Integer> data = new ArrayList<Integer>(); try { Scanner scanner = new Scanner(new File("data.txt")); while (scanner.hasNextInt()) { data.add(scanner.nextInt()); } scanner.close(); } catch (IOException e) { e.printStackTrace(); } // 统计数据 int sum = 0; int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (int i = 0; i < data.size(); i++) { sum += data.get(i); if (data.get(i) > max) { max = data.get(i); } if (data.get(i) < min) { min = data.get(i); } } double average = (double) sum / data.size(); // 绘制柱状图 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i = 0; i < data.size(); i++) { dataset.addValue(data.get(i), "Data", String.valueOf(i+1)); } JFreeChart chart = ChartFactory.createBarChart( "Data Distribution", "Index", "Value", dataset, PlotOrientation.VERTICAL, true, true, false ); try { ChartUtilities.saveChartAsPNG(new File("chart.png"), chart, 500, 300); } catch (IOException e) { e.printStackTrace(); } } } ``` 在这个示例程序中,我们从名为“data.txt”的文件中读取数据,并计算其总和、平均值、最大值和最小值。然后,我们使用JFreeChart库绘制一个柱状图,该图显示了每个数据点的值。最后,我们将图表保存为PNG图像文件“chart.png”。 注意:在运行此程序之前,你需要将JFreeChart库添加到你的Java项目中。你可以从JFreeChart官方网站下载最新版本的JFreeChart库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值